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.
Ex: void main()
{ int a=20,b=30,x;
int f1(int,int);
-----
x=f1(a,b);
-----
getch( );
}
int f1(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.
Ex: viod main()
{
int a=10,b=15,c=20;
------
X=f1(&a,&b,&c);
------
getch();
}
int f1(int *p1, int *p2, int *p3)
{
*p1=*p1+10;
*p2=*p2+5;
}
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.
Ex: void main()
{ int a=20,b=30,x;
int f1(int,int);
-----
x=f1(a,b);
-----
getch( );
}
int f1(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.
Ex: viod main()
{
int a=10,b=15,c=20;
------
X=f1(&a,&b,&c);
------
getch();
}
int f1(int *p1, int *p2, int *p3)
{
*p1=*p1+10;
*p2=*p2+5;
}