Making a C function return more than one value
There are two different methods used to pass parameters to a c function, call by value method and call by reference method. Call by value is simple, a copy of the values of actual arguments is passed to the calling function. Thus any changes made to the variables inside the function will have no effect on variables used in the actual arguments.
In the other method which is the call by reference method the address of a variable is passed. The content of that address can be accessed freely, either in the called or calling function. Therefore, the function called by reference can change the value of the variable used in the call.
Now normally a c function is unable to return more than one values to the calling function. This limitation can be overcome if we pass the address instead of the true value of the parameters. Then whenever we refer those parameters we do it with the address. Here, this simple c program will demonstrate this thing,
#include<stdio.h>
void func(int *a,int *b)
{
*a=*a+2;
*b=*b+2;
}
void main()
{
int x=12,y=13;
func(&x,&y);
printf(“After executing function:\nx=%d\ny=%d\n\n”,x,y);
}
The function func() does’nt even returning anything to main function. Unlike what we do in call by value, we’ve passed the address of x and y to func() from the main function. After the function is executed the value of these two variables are found to be modified. This is because we’ve referenced the variable with their address and not with their values. Thus any change made to the contents of those addresses are available to main function as well.Here is the output of this program.

Lets insert a few more printf statements in this program to monitor how and when the values have been changed in the process. The first printf statement in the main function will show the values before modification. The second printf shows the respective addresses.
#include<stdio.h>
void func(int *a,int *b)
{
printf(“Within function:\na= %p\nb= %p\n\n”,a,b);
*a=*a+2;
*b=*b+2;}
void main()
{
int x=12,y=13;
printf(“Before calling the function:\nx=%d\ny=%d\n”,x,y);
printf(“The address of x is: %p\n”,&x);
printf(“The address of y is:%p\n\n”,&y);
func(&x,&y);
printf(“After executing function:\nx=%d\ny=%d\n\n”,x,y);
}
Then we call the function and pass the addresses of the variables x and y. In call by value method we would have passed the values, not the addresses like we’ve done here. The control goes to func() and the addresses of x and y are stored in a and b respectively. The we use a fourth printf statement to demonstrate the values that are copied into a and b. In this function we have modified the values at those addresses. Each value is raised by 2. Then after coming back to main function we again use another printf to print the values of x and y. These values are found to be modified. The output of this will clearly show the modification process.

Note that I’ve used a %p in the printf statement. %p prints things in the format used by host computer. Here of course, it is Hexadecimal format.




