Where to Use Call by Reference in C

If you compare two parameter passing mechanisms, Call by Value and Call by Reference, in C programming language, Call by Value is much simpler in syntax and easy to understand. Then why should we use Call by Reference? Here we will try figure out what we can do with Call by Reference but can not do otherwise. If you can see the difference between Call by Value and Call by Reference and How they work .

1. Reduce overall memory requirement

You can reduce run time memory utilization of a program by using Call by Reference parameter passing mechanism instead of using Call by Value. Let consider this example.

#include  

typedef struct
{
    int a;
    int b;
    int c;
    int d;
} sample_struct;

void func1( sample_struct p)
{
    printf ("Size of p in func1 %dn", sizeof(p));
    printf ("Value of the members in func1 a=%d, b=%d, c=%d, d=%dn", p.a, p.b, p.c, p.d);
}

void func2( sample_struct * p)
{
    printf ("Size of p in func2 %dn", sizeof(p));
    printf ("Value of the members in func2 a=%d, b=%d, c=%d, d=%dn", p->a, p->b, p->c, p->d);
}

void main()
{
    sample_struct var1 = { .a = 1, .b = 2, .c = 3, .d = 4};
    
    printf ("Size of var1 in main %dn", sizeof(var1));
    func1(var1);
    func2(&var1);
}

Output:

Size of var1 in main 16

Size of p in func1 16
Value of the members in func1 a=1, b=2, c=3, d=4

Size of p in func2 4
Value of the members in func2 a=1, b=2, c=3, d=4

In this example, we have two functions func1 and func2 with similar functionality except func1 uses Call by Value mechanism and func2 uses Call by Reference. In the main function one variable var1 of type sample_struct is declared and the members of the structure variable are initialized with some values. As sample_struct structure has 4 integer members, the size of var1 will be 16 bytes on 32-bit system. Every integer takes 4 bytes. So the size of var1 will be printed 16 in the main the function. Lets see what happens after func1 call where Call by Value is used. After the call var1 will be copied to the parameter p of type sample_struct, i.e. the values of all members of var1 will be copied to the members of p.  The size of p will also be 16 bytes in the func1. That means during the execution of func1 two 16-byte variables will be created. In real life, the size of sample_struct can be arbitrarily large, even in mega or giga bytes. If Call by Value is used, the memory requirement will be doubled during the execution of the function.

Now we’ll see how we can avoid double memory requirement by using Call by Reference. When we call func2, we passed the reference or the pointer of the variable var1 instead of passing the whole structure to the function. The the size of p inside the func2 will will be 4 bytes in 32 bit system irrespective of the size of var1. But all members of var1 will be accessible through p as depicted in the example.

2. Get multiple outputs from a function

Another common usage of call by reference is to get multiple outputs from a function. C function can return maximum one variable from a function. If we want to get multiple outputs from a function, Call by Reference is the most common practice, though there are other mechanisms. In the simple example below it is shown how to get both summation and multiplication of two integers from a single function by using Call by reference mechanism. If you wanted to get only one output, either summation or multiplication then the function could simply return that. Here is the example

#include 

void func(int var1, int var2, int *out_sum, int *out_mult)
{
    *out_sum = var1 + var2;
    *out_mult = var1 * var2;
}

void main()
{
    int a = 5;
    int b = 10;
    
    int sum, mult;
    
    func(a, b, &sum, &mult);
    
    printf("Sum = %d, Multipication = %d", sum, mult);
}

Output:

Sum = 15, Multipication = 50

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

One thought on “Where to Use Call by Reference in C”

Leave a Reply

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

0
0
0
0
0
0