Function Call by Reference and Call by Value comparison – Computer Sir Ki Class

Code Learning #CPP#2680

## Function Call by Reference and Call by Value comparison

A program that uses both call by reference and call by value in the same program to compare it for better understanding.

## Learning Objectives

• Understanding call by reference and call by value, vis-a-vis each other with similar functionality but different approach.

## Source Code

 #include using namespace std; void increment(int &); int increase_and_return(int); int main() { int a=10; cout<<"Before referenced function call: a = "<

## Run Output

``````Before referenced function call: a = 10
After referenced function call: a = 11
Using call by value: a = 11
Using call by value: b = 12
``````

## Code Understanding

void increment(int &);
This is function prototype for call by reference. & denotes that the address of given variable location is to be passed. When the address is passed, the original value can be changed in the function also.

int increase_and_return(int);
This is function prototype of normal type call which is called the call by value call. Here the formal parameter in the function is passed and the variable scope of parameter is within the function only. The original value from where it is passed can not be changed by the called function.

int a=10; cout<<“Before referenced function call: a = “<<a<<endl;
Variable is set and displayed so that original value can be seen.

increment(a);
The function is called which is a call by reference function. Once it is called the value of a can be adjusted by the called function. The called function in this program is increasing the value passed.

cout<<“After referenced function call: a = “<<a<<endl;
This will print the incremented value. This should be 11 in this example.

int b=increase_and_return(a);
This function is also increasing the value by 1 but it is not changing the original value. The original value has become 11 in due to previous change in a so the returned value to b would be 12 but a will remain 11.

cout<<“Using call by value: a = “<<a<<endl;
cout<<“Using call by value: b = “<<b<<endl;
Both a and b as discussed above will be printed.

void increment(int &x) {++x;}
This is call by reference where the address location of x is being passed. This function modifies the original value as it has access to the original location.

int increase_and_return(int x){return ++x;}
This is call by value. It changes the value of x within its own scope and is not able to change the original value as it doesn’t have access to its address location. In turn it returns the modified value.

## Notes

• Call by reference gives advantage of making impact of functions on multiple variables at the same time but it has the disadvantage of accidentally and unknowingly change the values in the called functions.
• Call by value can return just one value at a time, but it makes the operation fool proof as called function can not modify the original values even accidentally.

## Common Errors

• Student sometime misunderstand the & sign. If they remember it as address of (&) operator they will understand that reference actually means location in the memory.

Suggested Filename(s): cbr-cbv.cpp

sunmitra| Created: 16-Jan-2018 | Updated: 16-Jan-2018|

×