Understanding the difference between pass by value and pass by reference in C

Let us understand the difference between pass by value and pass by reference in C.

Given below is a simple C program that illustrates the difference between ordinary arguments, which are passed by value, and pointer arguments, which are passed by reference.

#include<stdio.h>
void funct1(int a, int b);      /*function prototype*/
void funct2(int *pa, int *pb);   /*function prototype*/

void main()
{
   int a = 1;
   int b = 3;

   printf("\nBefore calling funct1: a=%d b=%d", a, b);
   funct1(a, b);

   printf("\nAfter calling funct1: a=%d b=%d", a, b);
   
   printf("\nBefore calling funct2: a=%d b=%d", a, b);
   funct2(&a, &b);

   printf("\nAfter calling funct2: a=%d b=%d", a, b);
}

void funct1(int a, int b)
{
   a = 0;
   b = 0;
   printf("\nwithin funct1: a=%d b=%d", a, b);
   return;
}

void funct2(int *pa, int *pb)
{
   *pa = 0;
   *pb = 0;
   printf("\nwithin funct2: *pa=%d *pb=%d", *pa, *pb);
   return;
}

The above function contains two function, called funct1 and funct2. The first function, funct1, receives two integer variables as arguments. These variables are originally assigned the values 1 and 3, respectively. The values are then changed to 0, 0 in funct1. The new values are not recognized in main, however, because the arguments were passed by value, and so any changes to the arguments are local to the function in which the changes occur.

Now consider the second function, funct2. This function receives two pointers to integer variable as its arguments. The arguments are identified as pointers by the indirection operators (i.e., the asterisk) that appear in the argument declaration. In addition, the argument declaration indicates that the pointers contain the addresses of integer quantities.

Within funct2, the contents of the pointer addresses are reassigned the values 0, 0. Since the addresses are recognized in both funct2 and main, the reassigned values will be recognized within main after the call to funct2. Therefore, the integer variables a and b will have their values changed from 1, 3 to 0, 0.

The six printf statements illustrates the value of a and b and their associated values *pa and *pb, within main and within the two functions. Hence, the following output is generated when the above mentioned program is executed:

Before calling funct1: a=1 b=3
within funct1:         a=0 b=0
After calling funct1:  a=1 b=3
Before calling funct2: a=1 b=3
within funct2:         *pa=0 *pb=0
After calling funct2:  a=0 b=0

Few more examples:
Program to add two numbers using function (Pass by value method)
Program to add two numbers using function (Pass by Reference)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet