Tuesday, 14 December 2010

Call By Reference

Addresses of the parameters are copied to the function. Whatever the modifications are done in function, those changes are reflects in calling function.

Use & (ampersand) operator is used in the actual parameter.
Advantages of the call by reference we can send more than one value from the function to calling function.

Ex: swap(&a,&b);

Example Program:
void main()
{
 void swap(int *,int*);
int a,b;
a=2;
b=3;
swap(&a,&b);
printf("a=%d\nb=%d\n",a,b);
}
void swap(int *x,int *y)
{
 int temp=x;
 x=y;
 y=temp;
}


Difference Between Call by value and Call by Reference


Call by value
Call by reference
1.       The variable/value/expression is sent to the function is known as call by value.
1.       If the address is sent to the function it is known as call by reference
2.       Syntax: function(variable,variable2);
3.       Syntax:function(&variable,&variable2);
4.here formal parameters is variable
Ex: intx
4.       Here formal parameters is pointers
Ex:int*x
5.       If the value is sent to the function ,that value cannot be modified
6.       Whenthe address is sent to the function the value is modified by using ‘*’operator




You may like the following posts:




Function call

No comments:

Post a Comment