
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass by Value vs Pass by Reference in C++
In C++, there are two main ways to pass arguments to functions: pass by value and pass by reference. When you use pass by value, a copy of the variable is made, so the original variable doesn't change. With pass by reference, the function works with the original variable, so it can be changed directly. Each method is used based on what you want the function to do.
We can pass arguments into a function in different ways. These different ways are ?
- Call by Value
- Call by Reference
- Call by Address
Sometimes, the call by address is referred to as call by reference, but they are different in C++. In call by address, we use pointer variables to send the exact memory address, but in call by reference, we pass the reference variable (alias of that variable).
This feature is not present in C; there, we have to pass the pointer to get that effect. In this section, we will see what the advantages of call by reference over call by value are and where to use them
Call by Value
In call by value, the actual value that is passed as an argument is not changed after performing some operation on it. When call by value is used, it creates a copy of that variable in the stack section in memory. When the value is changed, it changes the value of that copy, the actual value remains the same.
Where to use call by value?
- Use it to keep the original variable unchanged.
- Best for calculations or operations that don't affect the original data.
- Efficient for small data types like integers, floats, or characters.
Syntax
Following is the syntax to the Call by Value:
void function(int x) { x = x + 10; // Modifies the copy }
Example
In the following example, the function works on a copy of the variable, so the original variable remains unchanged after the function call.
#include <iostream> using namespace std; void callByValue(int x) { cout << "Before Adding Value in the Function: " << x << endl; x = x + 10; // Add 10 to the value cout << "After Adding Value in the Function: " << x << endl; } int main() { int value = 5; cout << "Before the Function Call: " << value << endl; callByValue(value); cout << "After the Function Call: " << value << endl; // Value remains unchanged return 0; }
Output
Following is the output to the above program:
Before the Function Call: 5 Before Adding Value in the Function: 5 After Adding Value in the Function: 15 After the Function Call: 5
Call by Reference
In call by reference the actual value that is passed as argument is changed after performing some operation on it. When call by reference is used, it creates a copy of the reference of that variable into the stack section in memory. Is uses a reference to get the value. So when the value is changed using the reference it changes the value of the actual variable.
Where to use Call by reference?
- The call by reference is mainly used when we want to change the value of the passed argument into the invoker function.
- One function can return only one value. When we need more than one value from a function, we can pass them as an argument.
- Ideal for returning multiple values from a function or working with large data types like arrays or objects.
Syntax
Following is the syntax to the Call by Reference:
void function(int &x) { x = x + 10; // Modifies the original variable }
Example
In this example, the function modifies the original variable directly, so changes made inside the function persist after the function call.
#include <iostream> using namespace std; void callByReference(int &x) { cout << "Before Adding Value in the Function: " << x << endl; x = x + 10; // Add 10 to the value cout << "After Adding Value in the Function: " << x << endl; } int main() { int value = 5; cout << "Before the Function Call: " << value << endl; callByReference(value); cout << "After the Function Call: " << value << endl; // Value is modified return 0; }
Output
The above program generates the following output:
Before the Function Call: 5 Before Adding Value in the Function: 5 After Adding Value in the Function: 15 After the Function Call: 15