Explain Call By Value and Call By Reference with example.
Call By Value : When a function call is given, a relationship is established between actual and formal parameters. A temporary storage is created for formal parameters to store the data(actual parameters) received as input from calling function. This process of data transfer from actual parameter to formal parameters is known as call by value.
Example :
int main()
{
int a=20,b=30,x;
int fun(int, int);
.....
x =fun(a, b);
.....
getch( );
}
int fun(int p, int q)
{
p = p+p;
q= q+q;
return(p+q);
}
Call By Reference : When a function is called, the address of variables are passed on to the called function. The parameters receiving the address should be pointer variables. This concept of sending the address of data from calling function to pointer variables in the called function is known as call by reference.
Example :
int main()
{
int a=1, b=5, c=2;
int x;
.....
x=fun(&a, &b, &c);
.....
getch();
}
int fun(int *p1, int *p2, int *p3)
{
*p1 = *p1 + 18;
*p2 = *p2 + 35;
*p3 = *p3 + 5;
return (*p1 + *p2 + *p3);
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: