Call by Value, Call by Reference & Call by Address (English)
Call by Value, Call by Reference & Call by Address (English)
learn the difference between call by value, call by reference, and call by address in
the C++ programming language.
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:- 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.
1
This is because both actual and formal parameters point to different
locations in memory (i.e. they both have different memory addresses).
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.
#include <iostream.h>
//Value of x gets copied into a
void increment(int a)
{
a++;
cout << "Value in Function increment: "<< a <<endl;
}
int main()
{
int x = 5;
increment(x);
cout << "Value in Function main: "<< x <<endl;
return 0;
}
***************************Output:********************************
2
Value in Function increment:6
Value in Function main:5
******************************************************************
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.
That means any change on one type of parameter will also be reflected by
other.
3
Program:- C++ Example implementing Call by Reference
#include <iostream.h>
//Value of x is shared with a
void increment(int &a)
{
a++;
cout << "Value in Function increment: "<< a <<endl;
}
int main( )
{
int x = 5;
increment(x);
cout << "Value in Function main: "<< x <<endl;
return 0;
}
******************************Output:******************************
Value in Function increment: 6
Value in Function main: 6
******************************************************************
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.
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.
4
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.
#include <iostream.h>
//a stores the address of x
void increment(int *a)
{
(*a)++;
cout << "Value in Function increment: "<< *a <<endl;
}
int main( )
{
int x = 5;
increment(&x); //Passing address of x
cout << "Value in Function main: "<< x <<endl;
return 0;
}
******************************Output:******************************
5
Value in Function increment: 6
Value in Function main: 6
******************************************************************
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.