Xpode.com        Click here to Print this article.

Function - Call by address

In this method the addresses of the actual arguments are copied to the formal arguments. Since the formal arguments point to the actual arguments as they contain addresses of the actual arguments, if we do any changes in the formal arguments they will affect the actual arguments. The formal arguments will be pointers because we have to store addresses in them.

For example (C++):

void swap(int *,int *);

void main ( )
int a,b;
clrscr ( );
cout<<"Before calling swap function: a="<<a<<" b="<<b;
swap (&a,&b);
cout<<"\nAfter calling swap function: a="<<a<<" b="<<b;
getch ();

void swap(int *x,int *y)
int temp;
cout<<"\nInside swap function: x="<<*x<<" y="<<*y;

Here in the main ( ) function we have declared two variables a and b with values 10 and 20 respectively. So we have in memory:

Call By Address

When we call the swap ( ) function by address, two pointer variables x and y are created and they contain the addresses of a and b respectively. So we have in memory now:

Call By Address

Inside the swap ( ) function we have interchanged the values of x and y. Since x and y point to a and b, any changes made to x and y will affect a and b. So after swap function we have in memory:

Call By Address


Contributed by:
Rohit kakria
I am software developer, moderator of xpode.com

Resourse address on xpode.com

Click here to go on website