top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

What is the difference between pass by value and pass by reference? Please explain with example?

0 votes
115 views
What is the difference between pass by value and pass by reference? Please explain with example?
posted Jun 2, 2015 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+1 vote
 
Best answer

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
answer Jun 2, 2015 by Shivaranjini
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...