Before understanding the difference between call by value, call by reference and call by address in C++. We first need to know, what are actual and formal parameters?

Actual Parameters: The parameters which appear in function call statements are known as actual parameters or arguments.

Formal Parameters: The parameters which appear in function declaration which has been called are known as formal parameters.

call by value and call by reference

Now let’s understand the different call mechanisms in C++.

Call by Value in C++

  • In Call by value when function is called, the value of the actual parameters is copied into the formal parameters.
  • Actual and formal parameters both have their own copies of values. Means that any change in one of the type of parameter will not be reflected by another.
  • It is because actual and formal parameters both points to different memory address location.
  • Call by value method is useful when we don’t want values of actual parameters to be changed by different functions.

Call by Value C++ Program

Output

Call by value c++ program output

Notice the output of the program. The value of ‘a’ has been incremented to 6,but the value of ‘x’ in main remains same. This proves that the value is being copied to a different memory location in the call by value method.

mechanism of call by value in C++

Call by Reference in C++

  • In Call by Reference, both formal parameter and actual parameter shares the same value.
  • Actual and Formal parameter points to same address in the memory location
  • It means that any change done by one type of parameter will be reflected by another type of parameter.
  • Call by Reference is used in those cases where we don’t want to make copies of object or variable, rather we want all operations to be performed on the same copy.
  • For making reference ‘&‘ is used in preceding of variable name.

Call by Reference C++ Program

Output

Call by Reference C++ Program Output

Notice the output in this case. The value of ‘a’ is incremented to 6, also the value of ‘x’ in main is changed to 6. This proves that the changes done in the formal parameter are also reflected by the actual parameter because they share the same memory address location.

mechanism of call by reference in C++

Call by Address in C++

  • In call by address method pointers are used as parameters. It is necessary to use pointer as formal parameter in order to implement call by address in C++
  • Since pointer is used, so the changes done by formal parameter will be reflected in actual parameter.
  • Actual and formal parameter points to different locations in memory but, since the formal parameter stores the address of actual parameter so they share the same value in memory.

Call by Address C++ Program

Output

Call by Address C++ program output

Here the output is the same as in the case of call by reference. Because the formal parameter is a pointer holding an actual parameter memory address. So the changes done by the formal parameter is actually being done in actual parameter memory location(where 5 is stored).

mechanism of call by Address in C++

As a conclusion, we can say that call by value should be used in those cases where we don’t 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 those cases where we want to maintain a single copy of one variable or object in the whole program. Hope you understood the difference between the function’s call mechanism in C++. If you have any doubts then comment below.

Leave a Reply

Close Menu