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.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>