There are two types of function calls:
1) Call by value: sending the values to the parameter.
2) Call by reference: sending the addresses to the parameter.
- In the first type value of each of the actual argument in the calling function is copied into the corresponding formal arguments (parameter) of the called functions. With this the changes made to the formal arguments in the called function have no effect on values of actual arguments in the calling function.
- In the second type, the addresses of actual arguments in the calling function are copied into the formal arguments of the called function .So, using these addresses, we would have access to the actual arguments and hence we would be able to manipulate them.
- Following programs illustrates call by value and call by reference:
Note: Using a call by reference, we can make a function return more than one value.
void swap(int n, int m);
void swapusingpointers(int *pn, int *pm);
int n1,n2; //n is an integer
n1 = 1;
n2 = 2;
printf("In main: %d %d\n",n1,n2);
void swap(int n, int m)
tmp = n;
n = m;
m = tmp;
printf("In Method: %d %d\n",n,m);
void swapusingpointers(int *pn, int *pm)
tmp = *pn;
*pn = *pm;
*pm = tmp;
printf("In Method: %d %d\n",*pn,*pm);
Mr. Sandeep Soni
Founder, Trainer & CEO, Deccansoft Software Services.
Sandeep has 21 yrs of experience working in various Microsoft Technologies/Platforms incl. VB6.0, ASP, VC++, VB.NET, C#. He is involved in managing and architecting projects at Deccansoft. He will be your liaison to Deccansoft, for any kind of communication and project updates. He knows what works and what doesn’t, and what practices are most suitable for design and programming with the ultimate goal of producing a quality system.