Showing posts with label call by reference. Show all posts
Showing posts with label call by reference. Show all posts

Saturday, May 29, 2010

GOOD EXAMPLE OF CALL BY REFERENCE VS CALL BY VALUE

How many times you get in a situation and think what to use in your function arguments- Call By Reference Or Call By Value?

This ain't tricky et al if you remember all basic simple thing i.e. In call by value, a function call passes arguments by value. The called function creates a new set of variables and copies the values of arguments into them. The function does not have access to the actual variables in the calling program and can only work on the copies of values. This mechanism is fine if the function does not need to alter the values of the original variables in the calling program. But, there may arise situation where we would like to change the values of variables in the calling program. One good example is Bubble Sort, we compare the two adjacent elements in the list and interchange their values if the first element is greater than the second. Now if the function is used for bubble sort, then it should be able to alter the values of variables in the calling function as well, which is not possible if the call by value method is used.
I'm sure this will help you remember the difference for your life now :)

Thursday, December 24, 2009

Call by Value and Call by Reference

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.

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

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.

P.S. In call by value the values may change in the calling function but those are not reflected in the main function but in the call by reference the calling function causes a change in the value of the arguments passed in the main function.