elibraryportal Logo

Call by Value and Call by Reference in C++

There are two types of function are available in C++

Call by Value and Call by Reference

Call by Value

In call by value, when you have passed the value to the function it is locally stored by the function parameter in stack memory location.If you change the value of function in parameter, it is changed for the current function only, but it not change the value of variable inside the caller function such as main().

Original value can not be modified.

Example:-

Output:

Value of a: 200
Value of b: 100

Call by reference

In call by reference, original value is changed or modified because we pass the reference (address). Here, address of the value is passed in the function, so actual and formal arguments shares the same address space.Hence, value changed inside the function, is reflected inside as well as outside the function.

Example:-

Output:

 Value of a: 200
Value of b: 100

Difference between call by value and call by reference.

SL NO.call by valuecall by reference
1.)This method copy original value into function as a arguments.This method copy address of arguments into function as a arguments.
2.)Changes made to the parameter inside the function have no effect on the argument.Changes made to the parameter affect the argument. Because address is used to access the actual argument.
3.)Actual and formal arguments will be created in different memory locationActual and formal arguments will be created in same memory location
Next Topic