In C++, call by value and call by reference are two methods that are used to pass an argument to a function. These methods are important to understand how the data is passed to the functions and how the changes in the variables are handled for efficient, safe, and maintainable C++ programs. In this article, we will discuss what is call by value and call by reference, their working, use cases, advantages, disadvantages, and differences in C++.
Table of Contents:
What is Call by Value in C++?
Call by value in C++ is a method of passing arguments to a function in the program. In this method, a copy of the original value is passed to the function. So, if any changes are made inside the function, it will not affect the original variable.
Syntax
The syntax of the call by value is:
void func(int x); // No special symbol
How Call by Value Works in C++
When a variable is passed to a function by value, then
- The value of the variable is copied.
- The function works on that copied value, and not on the original value.
- And the original value remains unchanged, even if the function changes the parameter.
Example:
Output:
The code shows how the value of the num is passed by value to modify the function, so the changes made inside the function do not affect the original variables. Thus, the num remains 5 outside the function.
When to Use Call by Value in C++
- You can use the call by value when you want to protect the original value from being modified.
- You can use this when you only need to read or work with a copy of the value.
- You must use the call by value when you have to work with small data types such as int, char, and float because copying them is fast and inexpensive.
- You should use this method when you want to ensure data safety and isolation inside the function.
Advantages of Call by Value in C++
- By using the call-by-value method, the original variable remains unchanged because the function works on a copy of the original variable.
- It is simple and safe to use.
- It is easier to debug and maintain because the function does not change the outside variables.
- Call by value is suitable for small data types such as int, char, or bool.
- It makes the code more predictable because the function’s behavior is isolated.
Disadvantages of Call by Value in C++
- The original variable cannot be modified within the function.
- The memory usage is increased because it makes a copy of each argument to work on.
- It gives low performance with working on large data types such as arrays or structures.
- It is not suitable for multiple return values.
- Call by value gives unnecessary overhead in some cases, even when there is no need to make a copy.
What is Call by Reference in C++?
Call by reference in C++ is a method of passing arguments to a function where the original memory address of the variable is passed. In this method, if any changes are made to the parameter directly, then it will affect the original variable.
Syntax
The syntax of the call by reference is:
void func(int &x); // Note the & symbol
How Call by Reference Works in C++
When a variable is passed by reference in the program, then
- No copy of the original variable is made.
- A reference to the original variable is used.
- And changing the parameter inside the function will also modify the original variable outside the function.
Example:
Output:
The code shows how the num is passed by reference to the modify function, so the changes made inside the function will directly affect the original variable. Thus, the num becomes 15 outside the function.
When to Use Call by Reference
- You can use the call by reference method when there is a need to modify the original variable.
- Use this method when you are passing large data types such as arrays, structs, or objects, which avoids the overhead of copying.
- You should use this method when you want to return multiple values from a function.
- You must use the call-by-reference method, as there is a need for good performance and efficiency.
Advantages of Call by Reference in C++
- The changes made in the function directly affect the original variable.
- It supports efficient memory usage because there is no need to copy the data, thus it saves memory.
- The call by reference method is used when working with large data types for fast processing.
- It supports returning multiple values.
- It reduces overhead and also improves speed and responsiveness.
Disadvantages of Call by Reference in C++
- Using call by reference sometimes leads to bugs if unintended changes occur.
- It is not that safe because it can affect the entire program if the error occurs in another part.
- It is difficult to find bugs and debug.
- Call by reference needs the & symbol to pass a variable by reference.
- It can cause unnecessary complexity if it is used for simple and small data types.
Get 100% Hike!
Master Most in Demand Skills Now!
Differences Between Call by Value and Call by Reference in C++
Below are a few main differences between call by value and call by reference in C++.
1. Parameter Passing: Value vs. Reference
Call by value passes a copy of the original variable to work on, while call by reference passes the address of the original variable to work on.
Example:
Output:
The code shows that the variable “a” is passed to callByValue, a copy of “a” is passed, and the function works on a separate value, while the variable “b” is passed to callByReference, the original address of “b” is passed, and the function works on the original value, and then the values are printed to the console.
2. How Call by Value & Reference Modify Variables
By using the call by value method, if the changes are made inside the function, it will not affect the original variable, and the original value remains unchanged. While using the call by reference method, if the changes are made inside the function, then it will directly affect the original variable, and the original value will be changed.
Example:
Output:
The code shows how the variable a is passed by call-by-value, and its original remains unchanged, while the variable b is passed by reference and its original value is changed because the function works on the original variable directly, and then the values are printed to the console.
3. Memory Efficiency: Call by Value vs. Reference
Call by value method needs more memory because it creates a copy of each variable that is passed to the function, and thus it is very expensive for large data types. While the call by reference method uses less memory because it does not create a copy.
Example:
Example:
The code shows how a new copy of the variable is created when it is passed by call by value, and the function uses a different memory address, while the same memory address is used by the function when the variable is passed by call by reference, and thus no extra memory is needed.
The call by value method gives slower performance when working with large data types such as arrays or structures, while the call by reference method gives faster performance when working with large data types because it avoids copying.
Example:
Output:
The code shows how the call by value method creates a copy of the entire vector, which takes more time and memory, while the call by reference method works directly with the original vector, which is much faster and efficient.
5. Safety Implications: Which Method is Safer?
Call by value is safer than call by reference because changes inside the function do not affect the remained program and the bugs can be handled easily.
Example:
Output:
The code shows how the variable is passed by the call by value remains unchanged and does not affect the entire program, while the call by reference changes the value of the variable b, and thus call by value is safer than the call by reference method.
6. Data Type Suitability for Value vs. Reference
Call by value is suitable for small data types such as int, char, etc., where copying the variables is cheaper and easier. While call by reference is suitable for large data types such as arrays, to avoid the copying overhead.
Example:
Output:
The code shows how the call by value method is good for the small data type int, as it easily modifies the value, and the call by reference method is good for large data type arrays, as it easily updates the array, and then the values are printed to the console.
Here is the table for a more precise understanding of the differences between call by value and call by reference.
Feature |
Call by Value |
Call by Reference |
What is Passed |
Passes a copy of the variable to the function |
Passes the address/reference of the variable |
Effect on Original Variable |
The original variable remains unchanged |
The original variable can be modified |
Memory Usage |
More memory (due to copying) |
Less memory (no copying) |
Performance |
Slower for large data types |
Faster for large data types |
Safety |
Safer (no accidental modification) |
Less safer than call by value (can change the original data) |
Data Type Suitability |
Small data types (int, char) where copying is cheap |
Large data types (arrays, objects) to avoid copying overhead |
Syntax |
void func(int x) |
void func(int &x) |
Conclusion
In C++, understanding the difference between call by value and call by reference methods for passing parameters is important for writing efficient and safe programs. The call by value method is used when there is a need to protect the original data and to create a copy inside the function, and the call by reference method is used when there is a need to modify the original variable and to avoid the overhead of copying the large data types. Thus, choosing the right method will help to improve the performance, memory usage, and code safety.
Difference Between Call by Value and Call by Reference in C++ – FAQs
Q1. What is call by value in C++?
Call by value in C++ is a method of passing arguments to a function in the program. In this method, a copy of the original value is passed to the function.
Q2. What is call by reference in C++?
Call by reference in C++ is a method of passing arguments to a function where the original memory address of the variable is passed.
Q3. Which method is safer?
Call by value is safer as it doesn’t allow the function to change the original data.
Q4. When should I use call by reference?
You should use call by reference when you need to modify the original variable, or when working with large objects, to avoid copying.
Q5. Does call by reference use more memory?
No, call by reference typically uses less memory because it avoids creating copies of large variables.