ASP ScriptingJava ScriptingCGI ScriptingHTMLJavaLanguage CERP Education Links

LANGUAGE C

Calling The Function by Reference

  Call by Reference

     In call by reference, the function is allowed access to the actual memory location of the argument and therefore can change the value the arguments of the calling routine. There may be, cases, where values of the arguments of the calling routine have to be changed.

     A function is written which takes two arguments interchanges their values and returns them.

     Function swap interchanges the values of u and v, but these values are not  passed back to the main(). As discussed earlier, this is because variables u and v in swap() are different from the variables u and v in main(). In a case like this, a call by reference can be used to achieve the desired result, because it will change the values of actual arguments. Pointers are used when a call by reference has to be made. 

#include <stdio.h>
main ( )
{
int x, y, *px, *py;
px=&x;
py=&y;
x = 15; y = 20;
printf("x = %d, y = %d\n", x, y); 
swap (px, p y) ;
printf("\nAfter interchanging x = %d, y = %d\n", x, y) ;
}
swap ( int *u, int *v) 
{
int temp; 
temp = *u; 
*u = *v;
*v = temp; 
return;
}

 


Your Ad Here
Not All Of Your Subscribers Use RSS - AWeber Email Marketing
Your Ad Here