When you call any function : there are two way to pass Value to it . Today we discuss benefits of these two approaches .
In this above example you can see first we call the function swap by method of call by value than than print its new values in function swap . But changing value in swap function does not alter value in main function .
you can see output ... . actual value in main function also changed .
Output for above two program is :
Thanks : Have a Nice Day
CALL BY Value :
In This Cause we just pass a copy of actual value and any change to this data in called function NOT change actual Value of The data in Caller function (which passes argument ).
Take an example : Swapping two number without using third variable .
- #include <stdio.h>
- void swap (int, int);
- int main ()
- {
- int a,b;
- printf("An Illustration of Call by value\n");
- printf("\nEnter the first number :\n");
- scanf("%d",&a);
- printf("\nEnter the second number :\n");
- scanf("%d",&b);
- printf("\nCall by Value.. \n");
- swap(a,b); //calling function and print new value in that func;
- printf("\nIn main function ..");
- printf("\nthe first number is %d \n",a); //printing value of a & b in main
- printf("\nthe second number is %d\n",b);
- return 0;
- }
- void swap (int x, int y)
- {
- x=x+y; //swapping without using third variable
- y=x-y;
- x=x-y;
- printf("\nNow the first number is : %d \n",x);
- printf("\nNow the second number is : %d \n",y);
- }
In this above example you can see first we call the function swap by method of call by value than than print its new values in function swap . But changing value in swap function does not alter value in main function .
Related Post : Php
crash course Get POST & REQUEST
CALL BY Reference :
In this method we pass address of variable to be passed . since now change made in values at this address obviously alter the value in caller function (here main )
Let us discuss above case : when we call by reference ....
- #include <stdio.h>
- void swap (int *, int *);
- int main ()
- {
- int a,b;
- printf("An Illustration of Call by reference\n");
- printf("\nEnter the first number :\n");
- scanf("%d",&a);
- printf("\nEnter the second number :\n");
- scanf("%d",&b);
- swap (&a,&b);
- printf("\nCall by Reference.. \n");
- printf("\nNow the first number is %d \n",a);
- printf("\nNow the second number is %d \n",b);
- return 0;
- }
- void swap (int *x, int *y) //receive address in pointer
- {
- *x=*x+*y; //swapping without using third variable
- *y=*x-*y;
- *x=*x-*y;
- printf("\nNow the first number is : %d \n",*x);
- printf("\nNow the second number is : %d \n",*y);
- }
you can see output ... . actual value in main function also changed .
Thanks : Have a Nice Day
Related Post : test
your programming skill P-2
My brother suggested I might like this web site. He was
ReplyDeleteentirely right. This post truly made my day. You cann't imagine just
how much time I had spent for this information! Thanks!
Look at my webpage: Louis Vuitton Sale
Have you ever considered about including a little bit more than just your articles?
ReplyDeleteI mean, what you say is important and everything. However think about if
you added some great pictures or videos to give your posts more, "pop"!
Your content is excellent but with images and videos, this website could certainly be one of
the greatest in its field. Awesome blog!
My weblog - Cheap New Balance
This is the right site for everyone who hopes to find out about this topic.
ReplyDeleteYou know so much its almost tough to argue with you (not that I personally would want to…HaHa).
You definitely put a fresh spin on a topic which has been discussed for many years.
Great stuff, just great!
My site - Christian Louboutin Heels
It's fantastic that you are getting thoughts from this article as well as from our discussion made
ReplyDeletehere.
Here is my blog :: Fake Louis Vuitton Bags
I am regular reader, how are you everybody?
ReplyDeleteThis post posted at this site is actually good.
my blog Beats By Dre
Hi there to all, how is everything, I think every one is getting
ReplyDeletemore from this website, and your views are pleasant designed for new
users.
Visit my site Louis Vuitton Online
Does your blog have a contact page? I'm having trouble
ReplyDeletelocating it but, I'd like to shoot you an email. I've got some ideas for your blog
you might be interested in hearing. Either way, great website
and I look forward to seeing it improve over time.
my web site Beats By Dre (http://beats-australia.rr32.com/)
Valuable information. Fortunate me I discovered your website by accident, and
ReplyDeleteI'm shocked why this twist of fate did not came about earlier!
I bookmarked it.
Also visit my webpage ... Dre headphones
With havin so much content and articles do you ever run into any problems
ReplyDeleteof plagorism or copyright infringement?
My website has a lot of completely unique content I've either
authored myself or outsourced but it seems a lot of it is popping it
up all over the web without my permission. Do you know any techniques to help protect against content
from being ripped off? I'd certainly appreciate it.
My blog post :: Replica Christian Louboutin