Search Your Topic

config
Tuesday, 13 June 2017 11:55

Call by value and call by reference in C++

Written by
Rate this item
(0 votes)

There are two ways to pass value or data to function in C language: call by value and call by reference. Original value is not modified in call by value but it is modified in call by reference.

CPP Call by value and call by reference in cpp 1

Let's understand call by value and call by reference in C++ language one by one.


Call by value in C++

In call by value, original value is not modified.

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

Let's try to understand the concept of call by value in C++ language by the example given below:

 
  1. #include <iostream>  
  2. using namespace std;  
  3. void change(int data);  
  4. int main()  
  5. {  
  6. int data = 3;  
  7. change(data);  
  8. cout << "Value of the data is: " << data<< endl;  
  9. return 0;  
  10. }  
  11. void change(int data)  
  12. {  
  13. data = 5;  
  14. }  

Output:

Value of the data is: 3

 

Call by reference in C++

In call by reference, original value is modified because we pass reference (address).

Here, address of the value is passed in the function, so actual and formal arguments share the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.

Note: To understand the call by reference, you must have the basic knowledge of pointers.

Let's try to understand the concept of call by reference in C++ language by the example given below:

 
  1. #include<iostream>  
  2. using namespace std;    
  3. void swap(int *x, int *y)  
  4. {  
  5.  int swap;  
  6.  swap=*x;  
  7.  *x=*y;  
  8.  *y=swap;  
  9. }  
  10. int main()   
  11. {    
  12.  int x=500, y=100;    
  13.  swap(&x, &y);  // passing value to function  
  14.  cout<<"Value of x is: "<<x<<endl;  
  15.  cout<<"Value of y is: "<<y<<endl;  
  16.  return 0;  
  17. }    

Output:

Value of x is: 100
Value of y is: 500   

Difference between call by value and call by reference in C++

No. Call by value Call by reference
1 A copy of value is passed to the function An address of value is passed to the function
2 Changes made inside the function is not reflected on other functions Changes made inside the function is reflected outside the function also
3 Actual and formal arguments will be created in different memory location Actual and formal arguments will be created in same memory location

 

Read 34 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « C++ Functions C++ Recursion »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.