GeekInterview.com
  I am new, Sign me up!
 
Home C
 

What is difference between call by value and call by reference in function?

 
Category: C
Comments (9)


The arguments passed to function can be of two types


1. Values passed
2. Address passed


The first type refers to call by value and the second type refers to call by reference.


For instance consider program1


main()
{
int x=50, y=70;
interchange(x,y);
printf(“x=%d y=%d”,x,y);
}

interchange(x1,y1)
int x1,y1;
{
int z1;
z1=x1;
x1=y1;
y1=z1;
printf(“x1=%d y1=%d”,x1,y1);
}


Here the value to function interchange is passed by value.


Consider program2


main()
{
int x=50, y=70;
interchange(&x,&y);
printf(“x=%d y=%d”,x,y);
}

interchange(x1,y1)
int *x1,*y1;
{
int z1;
z1=*x1;
*x1=*y1;
*y1=z1;
printf(“*x=%d *y=%d”,x1,y1);
}


Here the function is called by reference. In other words address is passed by using symbol & and the value is accessed by using symbol *.


The main difference between them can be seen by analyzing the output of program1 and program2.


The output of program1 that is call by value is


x1=70 y1=50
x=50 y=70


But the output of program2 that is call by reference is


*x=70 *y=50
x=70 y=50


This is because in case of call by value the value is passed to function named as interchange and there the value got interchanged and got printed as


x1=70 y1=50


and again since no values are returned back and therefore original values of x and y as in main function namely


x=50 y=70 got printed.


But in case of call by reference address of the variable got passed and therefore what ever changes that happened in function interchange got reflected in the address location and therefore the got reflected in original function call in main also without explicit return value. So value got printed as *x=70 *y=50 and x=70 y=50



Read Next: Convert a String into Long Value



 

 

Comments


nancysharma said:

  in call by value method,a compiler get a copy of the variable and thus changes made in the value in function will not reflected back to the called function.but in call by reference method,the compiler didn't get any copy ,but actually it works on the original copy and thus changes will be reflected back
May 18, 2008, 5:55 am

chittaranjan Nath said:

  frequently difference between the call by value and call by reference.
June 5, 2008, 4:56 am

Vipin said:

  // I think this isn very confusing issue.
Whatever i read in c or in C++. Actually Call By Ref(or Pass By Ref) is a new concept in C++
in which we pass values of actual arguments in Function Calling and in d Function Declarator of the Function Body we stored its val in d ref Variable( Formal Arguments). This is a situation b/w which is same syntax like CAll by Val bt Functionality equal 2 Call By Address..

void swap(int &, int&);
//fun call
Let us Suppose
a=50,b=30;
swap(a,b);

// outside main
void swap(int &a1,int &b1){

// Body of Swap

}
August 12, 2008, 3:24 am

udhayakumar said:

  2 types in passing arguments in c. 1.pass by value.2.pass by reference.
i.in call by value,if you pass a argument to a sub-function and you done furthur modification in sub-function,it won't affect the main function.
ii.above said,in call by reference,if you made a modification it affect the main function ..THIS IS THE MAIN DIFFERENCE
January 2, 2009, 6:23 am

SomGollakota said:

  Vipin -
Call By Value/Call By Reference is not new to C++. It has been around for a while. In C, we call/pass by value by passing variables, and we call/pass by reference by passing either pointers, or addresses of variables. See example below.
Eg:
main()
{
int myInt, *pMyIntPtr;
myInt = 10;
pMyIntPtr = &myInt; // assigning address of myInt to pMyIntPtr

MyCbyVal (myInt); // Call by value, or pass by value
MyCbyRef (pMyIntPtr); // Call/pass by reference (passing a pointer)
MyCbyRef (&myInt); // Call/pass by reference (passing address of myInt)
return myInt;
}

Any changes made to the parameter by the MyCbyVal function cannot be seen in the main function above. However, any changes made to the parameters of MyCbyRef (both statements) will be reflected in MyInt of main function, after the statements are executed.
March 8, 2009, 8:21 pm

sandeep said:

  In call by value we call a function by using it's value or by passing variable and in call by reference we call the value by using it's address.
March 23, 2009, 3:30 am

k.sivakrishna said:

  In call by value we call a function by using it's value and in call by reference we call the value by using it's address.
April 28, 2009, 1:29 am

mgvs said:

  call by value: the values are copied into the argument of the functions
May 18, 2009, 3:06 pm

archana said:

  call by value:when an argument passed by value, the date item copied to function.
call by reference:when an argument passed by reference then address of that data item passed to the function
June 6, 2009, 8:44 am

hardeepkaur said:

  this is the sufficient diference b/w these two methods.... thnx...
pass by address method is often used when manipulating aeeays and strings. This method is also used when we require multiple values to be returned by the calling function.
November 15, 2009, 5:20 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape