Ad Code

Responsive Advertisement

call by reference.

In call by reference the address of the variable are passed. In this value of variables are
affected by changing the value of the formal parameter.

void main()
{
       int a = 5;
       int b = 8;
      clrscr();
       printf("Before Calling the function a         and b are %d and %d \n", a,b);
     value(&a,&b);
      printf("After Calling the function a and     b   are %d and %d \n", a,b);
    getch();
}
value(int *p, int *q)
{
 (*p)++;
printf("In the function p and q are %d and %d \n", *p,*q);
}

/* Result of execution
Before Calling the function a and b are 5 and 8
In the function p and q are 6 and 9
After Calling the function a and b are 6 and 9
*/

-> In above example the address of a and b are passed to p and q which is a pointer
variable. (*p)++ means value at address is incremented. Similarly (*q)++ means value at address 2000, which is 8 incremented by 1

-> Now, the value of *p = 6 and *q = 9.
Here the address is not changed but value at this address is changed. Therefore after
calling the function the value of variables and b are changed.

Post a Comment

0 Comments