1. Introduction to C++: A Comprehensive Guide with Code Examples - 2023 2. Mastering Data Types in C++: A Comprehensive Guide with Codes and Examples (2023) 3. Learn About Variables and Types of Variables in C++ | Codzify.com 4. Control Statements in C++: A Comprehensive Guide for 2023 5. C++ Tutorial: Understanding Switch Statements with Codes and Examples in 2023 6. Understanding Memory Allocation and Pointers in C++: A Beginners Guide 7. Functions in C++ 8. Call by value and Call by Reference in C++ in depth 9. Array in C++ 10. 2d arrays in C++ 11. Classes and Objects in C++ 12. Static Functions in C++ 13. Constructors and Destructors in C++ - A Complete Guide with Examples 14. Mastering Copy Constructor in C++ - Shallow vs Deep Copy with Examples | Codzify 15. Understanding Friend Functions in C++ Made Simple! 16. Inline Functions in C++ 17. this Pointer in C++ 18. Mastering Inheritance in C++: Types and Examples Explained 19. Types of Inheritance in C++ 20. Polymorphism in C++ Explained with Codes and Examples in 2023 21. Templates in C++ 22. Getting the Value of a MultiMap in C++: Step-by-Step Guide with Examples 23. Multimap Find and Replace Operator in C++: Step-by-Step Guide - Codzify Topics 24. Exploring the Next_Permutation Algorithm without STL in C++ - Codzify Topics 25. C++ - The Difference Between Map and HashMap in STL 26. Updating Values in a std::multimap in C++ - Codzify Topics 27. Which data structure sorts the elements on insertion in C++ STL? 28. Can we implement Red Black Tree in c++ by STL containers? 29. How to Dynamically Declare an Array of Objects with a Constructor in C++ - A Step-by-Step Guide 30. What is the difference between a pointer and an object in C++? 31. Mastering Red-Black Trees with STLs Internal Implementation: A Step-by-Step Guide

Call by value and Call by Reference in C++ in depth

Article by: Manish Methani

Last Updated: October 20, 2021 at 8:04am IST
3 min 56 sec read

In case of callByValue value will not change after calling a function but in case of callByReference value will change after calling a function .

CallByValue just changes the value of the local variable. Local variables are the variables declared inside the block of function. So when we change the value inside a function there is a reflection but in the main function, it's not.

Whereas, CallByReference changes the value at the memory address. So when we change the value inside the function value gets directly changed at the address of that variable. That's why you see the reflection.

Call By Value

#include 
using namespace std;

void change(int a);

int main()
{
  int a = 5; 
  cout<<"Value of a before calling Function = "<after calling Function = " <before changing value of a = " << a << "
" ; // 5 
  a = 6;
  cout << "Value of a after changing value of a = " <

Output

Value of a before calling Function = 5
Value of a before changing value of a = 5
Value of a after changing value of a = 6
Value of a after calling Function = 5

Here as you see we changed the value of 'a' inside Change function as 6. But it's not reflected inside main Function.

Call By Reference

Example of Call by Reference :-

#include 
void change(int *a);
void main()
{
   int a = 5; 
   printf("Value of a before calling Function = %d",a );      // 5
  
/* Call by Value */ 
     change(&a);  
     printf("Value of a after calling Function = %d",a );    // 6
  }


void change (int *a )
{
    printf("Value of a before changing value of a = %d",*a ); // 5 
   /* Lets change the value of a*/
   a = 6;
   printf("Value of a after changing value of a = %d",*a ); // 6

}

Output

Value of a before calling Function = 5
Value of a before changing value of a = 5
Value of a after changing value of a = 6
Value of a after calling Function = 6

Here as you see we changed the value of 'a' inside Change function as 6. Yes, it is reflected inside main Function.

Why so ?

In case of callByValue value is not changed after calling a function but in case of callByReference value is changed after calling a function.

CallByValue just changes the value of the local variable. Local variables are the variables declared inside the block of function.So when we changed the value of 'a' inside a function there is a reflection but in the main function, it's not.

Whereas, CallByReference changes the value at the memory address. So when we change the value of 'a' inside the function value gets directly changed at the address of that variable. That's why you see the reflection.

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com