C Programming

Call By Value and Call by Reference in C Language

Function is a very important concept in C language. Function is piece of code which helps us to execute certain action. Based on its definition, function can be divided into four types.

This is based on function taking input and return output.

  1. Input none and output none.
  2. Input some value and output none.
  3. Input none and output some value.
  4. Input some value and output some value.

In today’s topic, we will discuss call by value and call by reference.  These topics are totally based on function’s classification.

Programming example 1:

In this programming example, we will see the mechanism of call by value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>

int add ( int, int ) ; // function declaration prototype.

int main ()

{

    int  s,  x,  y ;
    printf  (" Enter 2 numbers " ) ;
    scanf  ("%d %d ", &x, &y ) ;
    s = add (x , y) ;       // function call.
    printf ( " sum is %d ", s ) ;


return 0 ;

}

int  add (int a, int b) // definition of function and passing the value through function.

{

int c ;

c = a + b ;

return c ;

}

Output:

Explanation:

This programming example is the ideal example of call by value. It is the example of takes something and returns something.

Input some value and output none program is the same as the input none and output some value. The only difference is that, as it is by nature taking something, so the value of two numbers has to be passed in calling function. They are called actual arguments.

Or, user takes the value from keyboard that is used by main () function. The value then is passed to the add () function. These values are called formal arguments.

So, it is one type of called by value example. Otherwise, return procedure is the same as the Takes Nothing, Returns Something Procedure.

Programming Example 2:

Here we will see another example of Call by Value Method by applying Takes Something and Returns Nothing Procedure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>

void add ( int, int  ); // function declaration Globally

int main ()

{

   int x , y ;
   void add ( int, int ) ;              // function declaration locally


 printf ( " Enter 2 numbers " ) ;
  scanf ( " %d %d ", &x, &y ) ;     // Actual arguments
    add ( x , y ) ;    

return 0 ;

}

void add ( int x, int y ) // Formal arguments

{

    int c ;
    c= x + y ;
    printf ( " sum is %d ",c ) ;


}

Output:

Explanation:

If we want to add the two values of two variables that is declared inside the main (). Add this in a different function name add (). Then, we use Takes Something Returns Nothing method. In this case in main function, the value of x, y is passed to the add function when add () is calling. For this can be named as call by value.

Whenever we pass value as an argument to a calling Function, then these arguments are called Actual Arguments.

By definition, inside the parenthesis of a function when we declare the variable that will receive the value of variable that is passed by Calling Function is called Formal Argument.

The name of variable of Actual Argument and Formal Argument may be the same because compiler knows that variable x , y are declared inside main () function and x , y declared in add () are different variables.

In Calling Function, we just pass the value of variables that is declared inside main (). For this we write add (x , y) ;

Programming Example 3:

Here, we will see an example of Call by Address by applying Takes Something and Returns Something Procedure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>

int add ( int *, int * ) ; // function prototype declaration Globally

int main()

{

    int  s, x, y ;

    printf( " Enter Two numbers: " ) ;
    scanf("%d %d",&x,&y) ;
    s = add( &x, &y ) ;     // passing the address of variables.
    printf( " The Sum is: %d \n ", s ) ;
    return 0 ;


}

int add( int *a, int *b )

{

    int c ;
    c = *a + *b ;
    return ( c ) ;


}

Output:

Explanation:

This is an example of call by reference. Sometimes in our program a situation occurs when we are not able to pass the value of variable through function. We have to pass the address of these variables to access them. It is called call by reference.

Here, we pass the address of variable a, variable b inside the add function to sum the values of variable a, variable b.

Programming Example 4:

Here, we will see another example of Call by Address by applying Takes Something and Returns Something Procedure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>

int swap ( int *, int * ) ;

int main ()

{

   int p = 56 ;
   int q = 22 ;

    printf ( " Before swapping the value of integer p : %d \n ", p ) ;
    printf ( " Before swapping the  value of integer q : %d \n ", q ) ;


   swap( &p, &q ) ;     // call the function.

   printf( " After swapping the value of integer p : %d \n ", p ) ;
   printf( " After swapping the  value of integer q : %d \n ", q ) ;


return 0 ;

}

int swap( int *x, int *y ) // passing the address of variables.

{

   int t ;          /* temporary variable to store value of the 1st variable */

   t = *x ;
   *x = *y ;    
   *y = t ;


}

Output:

Explanation:

This is an example of Call by Reference. Sometimes in our program a situation occurs when we are not able to pass the value of variable through function. We have to pass the address of these variables to access them. It is called call by reference.

Here, we pass the address of variable a, variable b inside the swap () function to swap the values of variable a, variable b. As a result the values of a, b are interchanged through call by reference.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com