google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: Call by value and Call by refrence

Tuesday, July 8, 2014

Call by value and Call by refrence

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;
}

No comments:

Post a Comment

शिव भोलेनाथ स्तुति

 जय शिवशंकर, जय गंगाधर, करुणा-कर करतार हरे,   जय कैलाशी, जय अविनाशी, सुखराशि, सुख-सार हरे जय शशि-शेखर, जय डमरू-धर जय-जय प्रेमागार हरे,   जय ...