0% found this document useful (0 votes)
26 views6 pages

Call by Value, Call by Reference & Call by Address (English)

Call by value, Reference and call by Address
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Call by Value, Call by Reference & Call by Address (English)

Call by value, Reference and call by Address
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Call by Value, Call by Reference, and Call by Address in C++:- Here 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.

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.

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.

Program:- C++ Example implementing Call by Value

#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.

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.

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.

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.

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.

Program:- C++ Example implementing Call by Address

#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.

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.

You might also like