What is the difference between call by value and call by reference?

91

By Raaghavan

What is the difference between call by value and call by reference?

Hi friends, in this hub we will discuss about the difference between the call by value and call by reference. This is a very important question in any interview and I guess you will be confident with the solution of this question, when you read this hub.

Program 1 – Call by value

swap(int x1, int y1)

{

int z1;

z1=x1;

x1=y1;

y1=z1;

printf(“x1=%d y1=%d”,x1,y1);

}

main()

{

int x=100, y=200;

swap(x,y);

printf(“x=%d y=%d”,x,y);

}

Now let us follow the program. The program starts from main() and when the function swap(x,y) is called, it goes to statement swap(int x1, int y1). Now a variable z1 is declared and the reason for this declaration is to ensure that the swapping process takes place. So the following process takes place,

i) 100 get stored in z1.

ii) Now the value of 200 is stored in x1.

iii) Now z1, which contains 100 is stored in y1.

iv) So when the print statement is executed, we have x1=200, y1 = 100.

Now the function ends and the program is returned back to printf(“x=%d y=%d”,x,y); statement under main(). Now when this printf statement is executed, the output here is x=100, y=200.

So the general output for the program is,

x1=200 y1=100

x=100 y=200

How does this happen?


Now, let us discuss about the program. We can say this program is an example for "call by value". The reason is that when the function is called {when statement swap(x,y) is executed}, the program moves to the statement swap(int x1, int y1). Now in this case, when the function call takes place, the actual values of x and y is not passed to the function, swap (int x1, int y1). Only the copies of those values are passed to the function. This means that, calling a function using value, doesn't affect the main function, because we are not using the actual values, rather, we use the copy of these values. So in the function, the swapping process takes place and the values are displayed. Then when the process at the function is over, the program returns to the main function and here the values of x and y is displayed. Since we passed only the copy of the values to the function, we have the original data in the main () function and hence the output is displayed as x=100 y=200.

Program 2 – Call by reference

swap(int *x1, int *y1)

{

int z1;

z1=*x1;

*x1=*y1;

*y1=z1;

printf(“*x=%d *y=%d”,x1,y1);

}

main()

{

int x=100, y=200;

swap(&x,&y);

printf(“x=%d y=%d”,x,y);

}

The program above is an example for call by reference. Let us go through the program. The program starts from main () and when the function call is executed {when the program is in the statement, swap (&x,&y);}, the argument is passed by reference to call the function. Here, we use the symbol "&"(ampersand) in this function call. This "&" sign indicates that the address of the values x and y are passed to the function, and hence it is called as call by reference. Another important point is that, here the actual value is passed to the function (but in the case of call by value, we pass only the copy of the value) and this indicates that the main function no longer has the memory of the values.

In the function we use "*" and this is used to get the value of the variable from the address (as mentioned earlier, & is used for specifying the address). So * indicates the value corresponding to the address. After that the program continues and the swapping process takes place. So the first output will be, *x=200 *y=100.

Now when the function is over, the program is returned to the main function and directly to the

" printf(“x=%d y=%d”,x,y);" statement. Here the main function has no memory of the original value it had, and therefore, the main function's output depends on value which we got in the function, i.e. the second output will be x=200 y=100.

In general we can conclude that,

i) In call by value we pass the copy of the value of the function, but in call by reference, we pass the actual value, with address as reference.

ii) In call by value the values in the main function does not depend on any changes in the function, but in the case of cal by reference, any reflection of change in the function, will have an impact in the main function.


Comments

PM 19 months ago

Very useful information. Thanks

16 months ago

dsgdf

cv d 16 months ago

function,

thamo 15 months ago

more usefull provide many kinds of model prog....

shweta 5 months ago

very easy programming...

ank 4 months ago

thank you

Parvaiz raina 4 months ago

Thank you sir a good explination nd easy to undrstnd God bles u

RAHUL DOLIYA 3 months ago

thanks a lot

bushra 3 months ago

thanx alot

Rehana Anis 2 months ago

Thank you for the clear and simple explanation.

Shreya 2 weeks ago

very Informative!!!

amrit ojha 9 days ago

now m able to define the call by value and call by reference thank u.....so much......

ranjith 8 days ago

Thanku sir

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    • No HTML is allowed in comments, but URLs will be hyperlinked
    • Comments are not for promoting your Hubs or other sites

    Please wait working