Possible Duplicate:
Difference between value parameter and reference parameter ?
What is the difference between call by reference and call by value?
Possible Duplicate:
Difference between value parameter and reference parameter ?
What is the difference between call by reference and call by value?
In C, there is no call by reference. The closest you can get is taking an address, and passing a copy of that address (by value -- see below).
In C++, call by reference passes a reference to an object (an alias for the original object). Generally this will be implemented as the object's address, though that's not guaranteed.
Call by value means taking a value of some sort, and passing a copy of that value to the function.
The basic difference is that when you pass a parameter by value, the function receives only a copy of the original object, so it can't do anything to affect the original object. With pass by reference, it gets a reference to the original object, so it has access to the original object, not a copy of it -- unless it's a const reference, it can modify the original object (for one example).
Briefly, call by reference is when a function can modify its arguments:
def f(x):
x := x + 1
print(x)
x := 1
f(x)
/* Now, x is 2 */
print(x)
In "call by reference", the above will print 2
twice.
Call by value is when a function receives a copy of the argument passed to it, so any modifications don't get seen by the caller. With f(x)
defined as above, call by value would be:
x := 1
f(x)
/* x is 1 in the caller */
print(x)
In the above, inside f(x)
, the print call would print 2
, but that only modifies the copy of x
that f()
got, so in the caller, x
is still one, and the second print()
prints 1
.
The arguments passed to function can be of two types namely 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.
Call by value:
void foo( int x ) {
cout << x << endl;
x = 4;
}
int someVar = 5;
foo( someVar );
The value 5 is pushed onto the stack and foo is called. Inside foo(), 5 is popped off the stack and output. x = 4 modifies the stack copy, which is then thrown away when the function returns.
void foo( int& x ) {
cout << x << endl;
x = 4;
}
int someVar = 5;
foo( someVar );
The address of someVar is pushed onto the stack and foo is called. Inside foo, the address is popped off the stack and the integer stored at that address is output. x = 4 modifies the memory referred to by the address, which means that when foo() returns, someVar now has the value 4.
Contrast the above code to
void foo( int* x ) {
cout << *x << endl;
*x = 4;
}
int someVar = 5;
foo( &someVar );
This code does the exact same thing as my reference example, it's just the syntax is a bit different: note the &someVar where foo is called, and note the *x in foo() to refer to the integer value.
In call by reference you will get the instance of a variable. After changing parameters of function, in this case your variable will change in call method. In call by value you will get the copy of the instance. And in this case changes of parameter variable will not influence on variable that was in call method