Free Training
C Language   |   CSS   |   MainFrame   |   VBScript   |   PHP   |   XML   |   C++ Tutorials   |   Ajax   |   JavaScript   |   CSS3   |   UML   |   jQuery   |   Microsoft AJAX

Sponsored Links

C Language Tutorials

 
Home Tutorials C Language
 

Call by Value and Call by Reference

 

Call by Value and Call by Reference

In this tutorial you will learn about C Programming - What is difference between call by value and call by reference in function?



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



Read Next: Concept of Pixel in C Graphics



 

 

Comments


sivauuuuuuuuuuuuuuuuugyt said:

  excllent
December 13, 2007, 8:39 am

jamzx said:

  nice,..................have a great logic!!!!
October 7, 2008, 5:15 am

Abhishek Joshi (H7) said:

  its good to see the proper coding structure.....

what else i can comment on this...
no mistakes.....
waiting for more....
December 18, 2008, 2:47 pm

kuldeep said:

  The programs are very easy to understand but my program is not executing while running program, its giving an errorof prototype of interchange. pls give me suggestions.
March 21, 2009, 2:51 am

gyanraushan said:

  it is very easy to know about call by reference and call by value but i can be make more usefriendly by using comment and memory changes of value at runtime
May 28, 2009, 9:29 am

Prasanth Chandran PK said:

  Simple and Understadable examples, Waiting for More
June 4, 2009, 5:12 am

pravi6 said:

  Excellent.......Thanks.......
July 4, 2009, 11:25 am

Post Your Comment:

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

Sponsored Links