Summary: In this tutorial, we will learn the difference between call by value, call by reference, and call by address in the C++ programming language.

To understand the importance of each type of call and their difference, we must know, what the actual and formal parameters are:

Actual Parameters are the parameters that appear in the function call statement.

Formal Parameters are the parameters that appear in the declaration of the function which has been called.

call by value and call by reference

Now let’s look at how each call mechanism works.

Call by Value

When a function is called in the call by value, the value of the actual parameters is copied into formal parameters.

Both the actual and formal parameters have their own copies of values, therefore any change in one of the types of parameters will not be reflected by the other.

This is because both actual and formal parameters point to different locations in memory (i.e. they both have different memory addresses).

mechanism of call by value in C++

Call by value method is useful when we do not want the values of the actual parameters to be changed by the function that has been invoked.

C++ Example implementing Call by Value

Output:

Call by value c++ program output

Note the output of the program. The value of ‘a’ has been increased to 6, but the value of ‘x’ in the main method remains the same.

This proves that the value is being copied to a different memory location in the call by value.

Call by Reference

In the call by reference, both formal and actual parameters share the same value.

Both the actual and formal parameter points to the same address in the memory.

mechanism of call by reference in C++

That means any change on one type of parameter will also be reflected by other.

Calls by reference are preferred in cases where we do not want to make copies of objects or variables, but rather we want all operations to be performed on the same copy.

C++ Example implementing Call by Reference

Output:

Call by Reference C++ Program Output

Note: For creating reference, the ‘&‘ operator is used in preceding of variable name.

Note the output in this case. The value of ‘a’ is increased to 6, the value of ‘x’ in the main also changes to 6.

This proves that changes made to formal parameters are also reflected by the actual parameters as they share the same memory address space.

Call by Address

In the call by address method, both actual and formal parameters indirectly share the same variable.

In this type of call mechanism, pointer variables are used as formal parameters.

The formal pointer variable holds the address of the actual parameter, hence the changes done by the formal parameter is also reflected in the actual parameter.

mechanism of call by Address in C++

As demonstrated in the diagram, both parameters point to different locations in memory, but since the formal parameter stores the address of the actual parameter, they share the same value.

C++ Example implementing Call by Address

Output:

Call by Address C++ program output

The output here is the same as in the case of call by reference i.e. the value of both ‘a’ and ‘x’ changes.

It is clear that changes made by the formal parameters are also changes the actual parameter value.

Conclusion

As a conclusion, we can say that call by value should be used in cases where we do not want the value of the actual parameter to be disturbed by other functions and call by reference and call by address should be used in cases where we want to maintain a variable or a copy of the object throughout the program.

Hope you understood the different type of call mechanism in the C++ programming language. Comment below if you have any doubts.

This Post Has One Comment

  1. Satya Narayana meena

    Adrass number ka

Leave a Reply

5 × four =