Difference between Call by value and Call by Reference in C++
Call by Value and Call by Reference in C++: An Overview
If you're new to coding, you have probably heard of call by value and call by reference in C++ programming language. But what are they exactly? In this blog post, we're going to take a look at the basics of both concepts in C++. You can also explore C++ online training so that even novice coders can better understand them. We'll also discuss when it's best to use one or the other while writing code in C++, ensuring that your program runs efficiently and correctly. So whether you've been wanting to learn more about these principles or simply brush up on your knowledge, fear not—we've got all the details right here!
Ways of Passing the Data to the Function
There are two ways to pass the data to the function in the C++ language:
- Call by value in C++ language
- Call by reference in C++ language
What is Call by Value in C++?
Call by value in C++ is a programming method that passes the value of a variable to a function as an argument, instead of passing the memory address of the variable.
- In call by value, the values of arguments are copied into local variables, and any changes made to that value while within the scope of the function do not affect in original values stored within the calling program.
- By preventing unexpected data changes, call by value assures programme stability, resulting in robust and economical development.
Example
#include <iostream>
using class std;
void change(int data);
int main()
{
int data = 4;
change(data);
cout << "The value of the data is: " << data<< endl;
return 0;
}
void change(int data)
{
data = 5;
}
The code attempts to modify the value of the variable "data" within the "change" function, but "data" is given by value in "main," generating a local copy that doesn't change the original variable.
Output
The value of the data is: 4
What is Call by Reference in C++?
Call by reference in C++ is a powerful method of passing arguments to functions.
- It allows objects that are passed as arguments to reference their original counterpart in the callers' scope, allowing manipulation of arguments at both call time and return time.
- This method allows for a unique set of operations that call by value cannot, such as fixing values to be used between many interrelated calls, or even returning multiple values from one call.
- With its ability to give many advantages over a call by value, call by reference in C++ stands out as being very advantageous when programming certain algorithms.
Example
#include<iostream>
using class std;
void swap(int *a, int *b)
{
int swap;
swap=*a;
*a=*b;
*b=swap;
}
int main()
{
int a=4500, b=1300;
swap(&a, &b); // passing value to function
cout<<"The value of a is: "<<a<<endl;
cout<<"The value of b is: "<<b<<endl;
return 0;
}
This C++ code defines the ' swap' function, which swaps the values pointed to by two integer pointers as input. By giving the addresses of 'a' and 'b' to ' swap,' it switches the values of 'a' and 'b,' which results in 'a' containing 1300 and 'b' containing 4500, which are then shown.
Output
The value of a is: 1300
The value of b is: 4500
Difference between Call by Value and Call by Reference in C++
There are some major differences between Call by Value and Call by Reference:
Aspect | Call by Value | Call by Reference |
What is passed | Copies the actual value of the argument. | Passes a reference or memory address of the argument. |
Modification of argument | Changes made to the parameter inside the function do not affect the original argument. | Changes made to the parameter inside the function directly affect the original argument. |
Memory Usage | Typically consumes less memory as it creates a separate copy of the argument. | May consume more memory as it uses references to the original data. |
Performance | Generally faster due to the smaller data transfer. | May be slower due to the need to access data through references. |
Data Type Constraints | Suitable for simple data types (int, float, etc.). | Suitable for complex data types (arrays, objects, etc.). |
Risk of unintended changes | Reduces the risk of unintentional side effects. | Increases the risk of unintentional side effects. |
FAQs
1. What is call by value and call by reference in C++?
2. What is the syntax of call by value?
In C++, call by value is expressed using the syntax void functionName(dataType parameterName).
3. What is Call by Value and Pass by Value?
In C++, passing a copy of the argument to a function is referred to as calling it by value or passing it by value.
4. What is Call by Value with example?
Example of a call by value: void modifyValue(int x) x = 42; where the copied argument is represented by x.
5. What is Call by Reference with example?
Example of a call by reference: void modifyValue(int &x) x = 42; where x references directly to the initial argument, allowing alterations to have an immediate impact on it.
Summary
If you are a C++ programmer, it is important to understand the call by value and call by reference in C++ difference, call by value and call by reference in c++ program, you can learn more in our C++ Certification. This article contains the difference between call by value and call by reference. The main difference between these two methods is that with a call by reference, changes made to the parameter will be reflected in the argument, while this is not the case with the call by value. Be sure to keep this distinction in mind when programming in C++!
Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.