Call by reference and Call by value

Written by

Utkarsh Shukla

What is Call by Value and Call by Reference ?

Since, we all know that functions are an important part of any program/code, written in any programming language. There are various types of function calling in any programming language.

One of its part is argument calling function.

In this type we can see various types of categories such as Call by Value, Call by function, Call by result etc.
Here, we will be talking about the two important ones and will be comparing them.

Call by Value

This method uses inner mode semantics. Inner mode semantics means that when we make any changes in the initial parameter, then it doesn’t get reflected in the called parameter.

This could further be elaborated as, if we make any changes/modification in the functional block(function which has call by value functionality), this would only affect the storing memory of the function, rather than affecting the calling environment.

So, in short in call by value, original value is not modified.

Example :

#include <stdio.h>
        #include <conio.h>
          void swap(int a, int b) {
            int temp;
            temp = a;
            a = b;
            b = temp;
          }
        void main() {
          int a = 1, b = 2;
          clrscr();
          swap(a, b); // passing parameters
          printf("\nValue of a: %d", a);
          printf("\nValue of b: %d", b);
          getch();
        }

Through the above code, we can see the functionality of Call by value functions.

Disadvantages-
1. It is not effective and much more costly for the arrays and object calling.
2. It takes very much memory for storage allocation.

Call by Reference

This method uses inner & outer both mode of semantics. In this method, changes made to the initial parameter gets reflected back to the caller through parameter passing.

When any changes are made in the initial parameter, these changes are reflected in the actual parameter in the calling environment as initial parameter receives a reference (or pointer) to the actual data. Which means the address of the variable is used in the storage allocation.

This method up somes the shortcomings of Call By Value method, as it is efficient in time as well as space complexity.

So, in short in call by reference, the original value is modified because we pass a reference (address).

Example:

#include <stdio.h>
        #include <conio.h>
          void swap(int * a, int * b) {
            int temp;
            temp = * a;
            * a = * b;
            * b = temp;
          }
        void main() {
          int a = 1, b = 2;
          clrscr();
          swap( & a, & b); // passing value to function
          printf("\nValue of a: %d", a);
          printf("\nValue of b: %d", b);
          getch();
        }

Disadvantages
1. Potentially unstable.
2. Difficult to understand.

The major difference between these two are as follows-

1. Call by Value is treated as an asset whereas Call by reference is treated like a liability.

2. Program in Call by Value is easily understandable and compact, whereas Program in Call by reference are more complex and big.

3. Every problem can not be solved by Call by Value method, whereas every problem can be solved by Call by reference method.

4. For large and redundant value calling programs Call by Reference is more preferable.

5. In call by value, actual arguments will remain safe, they cannot be modified/change accidentally whereas In call by reference, alteration to actual arguments is possible within from called function; therefore the code must handle arguments carefully else you get unexpected results.

Call by reference and Call by value