Introduction to C++ | Call by Value & Call by Reference
Call by Value and Call by Reference
In C++, there are two ways to pass arguments to functions: Call by Value and Call by Reference. Both techniques determine how function arguments are passed and how changes to those arguments affect the original data in the calling function.
What is Call by Value?
In Call by Value, a copy of the actual parameter is passed to the function. The function works with the copy of the data, and any modifications made to the parameter inside the function do not affect the original argument in the calling function.
Call by Value Syntax
Call by Value Syntax
return_type function_name(type parameter) {
// code body
// function uses a copy of the actual argument
}
Example: Call by Value
In the following example, the add()
function is called by value, so the original value of the variable x
remains unchanged after the function call.
Code Example: Call by Value
#include <iostream>
using namespace std;
// Function to add 10 to a number
void addTen(int num) {
num = num + 10; // Modifying the copy of the argument
cout << "Inside function, num = " << num << endl;
}
int main() {
int x = 5;
addTen(x); // Call by value
cout << "Outside function, x = " << x << endl; // x remains unchanged
return 0;
}
Output
Outside function, x = 5
Explanation of Code
In the example, x
is passed by value to the addTen()
function. The function works with a copy of x
, so changes made to num
inside the function do not affect the original variable x
outside the function.
What is Call by Reference?
In Call by Reference, the actual memory address (or reference) of the argument is passed to the function. This means that the function operates directly on the original data, and any changes made inside the function will affect the original argument in the calling function.
Call by Reference Syntax
Call by Reference Syntax
return_type function_name(type ¶meter) {
// code body
// function works with the original argument, not a copy
}
Example: Call by Reference
In the following example, the addTen()
function is called by reference, so changes to the variable x
inside the function will be reflected outside the function as well.
Code Example: Call by Reference
#include <iostream>
using namespace std;
// Function to add 10 to a number (Call by reference)
void addTen(int &num) {
num = num + 10; // Modifying the original argument
cout << "Inside function, num = " << num << endl;
}
int main() {
int x = 5;
addTen(x); // Call by reference
cout << "Outside function, x = " << x << endl; // x is modified
return 0;
}
Output
Outside function, x = 15
Explanation of Code
In this example, x
is passed by reference to the addTen()
function. The function modifies the original variable x
because the function works with a reference to the original data. Therefore, the change is reflected outside the function as well.
When to Use Call by Value vs Call by Reference?
Here are some general guidelines on when to use each method:
- Call by Value: Use this method when you do not want the function to modify the original variable. It is useful for small, simple data types (e.g., integers) where performance is not a concern.
- Call by Reference: Use this method when you need the function to modify the original argument. It is especially useful for larger data structures like arrays or objects, as it avoids copying the data and can improve performance.
Pro Tip:
💡 Pro Tip
Use Call by Reference when dealing with large data types like arrays or custom objects, as it avoids the overhead of copying the data. However, be cautious with Call by Reference, as changes made in the function will affect the original variable, which may lead to unintended side effects.