Passing pointers to functions

Arguments can generally be passed to functions in one of the two following ways:

1. Pass by value method
2. Pass by reference method

In the first method, when arguments are passed by value, 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 argument list.

However, when arguments are passed by reference (i.e. when a pointer is passed as an argument to a function), the address of a variable is passed. The contents 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.

Here is a simple program that illustrates the difference.

Write a program to swap the values using the pass by value and pass by reference methods.

/* Program that illustrates the difference between ordinary arguments, which are passed by value, and pointer arguments, which are passed by reference */
# include <stdio.h>
main()
{
int x = 10;
int y = 20;
void swapVal ( int, int ); /* function prototype */
void swapRef ( int *, int * ); /*function prototype*/
printf("PASS BY VALUE METHOD\n");
printf ("Before calling function swapVal x=%d y=%d",x,y);
swapVal (x, y); /* copy of the arguments are passed */
printf ("\nAfter calling function swapVal x=%d y=%d",x,y);
printf("\n\nPASS BY REFERENCE METHOD");
printf ("\nBefore calling function swapRef x=%d y=%d",x,y);
swapRef (&amp;x,&amp;y); /*address of arguments are passed */
printf("\nAfter calling function swapRef x=%d y=%d",x,y);
}
/* Function using the pass by value method*/
void swapVal (int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
printf ("\nWithin function swapVal x=%d y=%d",x,y);
return;
}
/*Function using the pass by reference method*/
void swapRef (int *px, int *py)
{
int temp;
temp = *px;
*px = *py;
*py = temp;
printf ("\nWithin function swapRef *px=%d *py=%d",*px,*py);
return;
}

OUTPUT

PASS BY VALUE METHOD

Before calling function swapVal x=10 y=20
Within function swapVal x=20 y=10
After calling function swapVal x=10 y=20

PASS BY REFERENCE METHOD

Before calling function swapRef x=10 y=20
Within function swapRef *px=20 *py=10
After calling function swapRef x=20 y=10

This program contains two functions, swapVal and swapRef.

In the function swapVal, arguments x and y are passed by value. So, any changes to the arguments are local to the function in which the changes occur. Note the values of x and y remain unchanged even after exchanging the values of x and y inside the function swapVal.

Now consider the function swapRef. This function receives two pointers to integer variables as arguments identified as pointers by the indirection operators that appear in argument declaration. This means that in the function swapRef, arguments x and y are passed by reference. So, any changes made to the arguments inside the function swapRef are reflected in the function main( ). Note the values of x and y is interchanged after the function call swapRef.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: