Functions in C++ receive arguments either by call by value or call by reference. These parameter passing methods determine whether a function works on a copy of the data or the original variable.
- Call by Value passes a copy of the argument to the function.
- Call by Reference passes a reference to the original variable, allowing direct modification.
Actual Parameters vs Formal Parameters
Before understanding these methods, it is important to know the two types of function parameters.
- Actual Parameters (Arguments): Values passed during the function call.
- Formal Parameters: Parameters declared in the function definition that receive the arguments.
For example:
#include <iostream>
using namespace std;
void display(int x) { // x is a formal parameter
cout << x;
}
int main() {
int num = 10;
display(num); // num is an actual parameter
return 0;
}
Call by Value
In call by value, the function receives a copy of the original variable. Changes made inside the function affect only the copied value, leaving the original variable unchanged.
- Keeps the original variable unchanged after the function call.
- Suitable for small data types such as int and char.
- Useful when the function only needs to read the value.
#include <iostream>
using namespace std;
// Function receives a copy
void increment(int num)
{
num++;
cout << "Inside function: " << num << endl;
}
int main()
{
int number = 5;
increment(number);
cout << "Outside function: " << number << endl;
return 0;
}
Output
Inside function: 6 Outside function: 5
Explanation
- number is passed by value to increment(), a copy of number is stored in num.
- Incrementing num changes only the copied value and the original variable number remains unchanged.
Call by Reference
In call by reference, the function receives a reference to the original variable instead of a copy. As both parameters refer to the same memory location, changes made inside the function directly modify the original variable.
- Avoids copying large objects, improving performance.
- Allows the function to modify the original variable.
- Commonly used for large objects or output parameters.
#include <iostream>
using namespace std;
// Function receives a reference
void increment(int& num)
{
num++;
cout << "Inside function: " << num << endl;
}
int main()
{
int number = 5;
increment(number);
cout << "Outside function: " << number << endl;
return 0;
}
Output
Inside function: 6 Outside function: 6
Explanation
- number is passed by reference using int&, num becomes another name for number.
- Incrementing num directly modifies the original variable and the updated value is visible after the function returns.
Call by Value Vs Call by Reference
| Feature | Call by Value | Call by Reference |
|---|---|---|
| Value Passed | A copy of the variable is passed. | A reference to the original variable is passed. |
| Effect on Original Variable | Original value remains unchanged. | Original value is modified if changed inside the function. |
| Memory Usage | Requires additional memory for the copy. | No additional copy is created. |
| Performance | Slightly slower for large objects due to copying. | More efficient for large objects. |
| Memory Address | Formal and actual parameters have different addresses. | Both refer to the same memory location. |
| Use Cases | When the original value should remain unchanged. | When the function needs to modify the original variable or avoid copying. |