C++ Function Call By Value
Last Updated :
15 Sep, 2023
A function is a collection of statements that accept inputs, carry out certain calculations, and output the results. The concept is to group similar or often performed actions into a function so that we may call the function rather than writing the same code again for various inputs. A function is a section of code that executes only when it is called, to put it simply.

Function in C++
Call by Value
When a function is called, new elements are created on the stack memory to store the essential information about the functions as well as allocated memory space for parameters and the return value.
The call-by-value method allows you to copy the actual parameter to a formal parameter. In this case, if we change the formal parameter then the actual parameter doesn’t change.
In other words, the value of the parameter is duplicated into the memory location designated for the function’s parameter. Consequently, two memory locations now hold the same value. C++ uses call by value method by default.
Upon exiting the function, the memory on the program stack is ‘popped,’ leading to the removal of all data associated with the function call. This includes the memory location allocated for the parameters used within the function. Consequently, any alterations made to values within the function’s scope do not impact the values of variables outside the function.
Use of Call by Value
Call by value is used in certain conditions like:
- When you do not want to change the actual parameters of the function.
- When you want to make copies of the data instead of the actual data.
- When space is not an issue.
- Usually, when you do not deal with recursion or backtracking.
Limitations of using Call by Value
Although call by value is so useful for us while programming in C++, it has a few limitations too:
- Data passed is then stored in temporary memory
- Can’t operate over the actual data rather need to work on temporary copy made of the data, because of which changes made in the data in the function is not reflected in main function.
- Memory Space required while using string ,array , vector, etc can be huge.
- Tackling Backtracking and recursion can be complex using call by values.
Example 1:
C++
#include <iostream>
using namespace std;
void swap( int x, int y)
{
int t = x;
x = y;
y = t;
cout << "After Swapping in function x: " << x
<< ", y: " << y << endl;
}
int main()
{
int x = 1, y = 2;
cout << "Before Swapping: " ;
cout << "x: " << x << ", y: " << y << endl;
swap(x, y);
cout << "After Swapping: " ;
cout << "x: " << x << ", y: " << y << endl;
return 0;
}
|
Output
Before Swapping:
x: 1, y: 2
After Swapping in function x:2, y:1
After Swapping:
x: 1, y: 2
As, you can see we pass two values in the swap functions x and y i.e., 1 and 2 before and after swapping them through a call-by-value method, if, we print the value of x and y then it is not changed coz the function swap create copies of x and y and swap them and their scope is inside that function only and the actual x and y values have not been modified by the swap function that why it is called call by value. If you want that the changes also reflect in the main function then you have to call it by reference and for that, you have to append the ‘&’ symbol before x and y in the formal argument.
Example 2:
C++
#include <iostream>
using namespace std;
int fun( int x)
{
x += 1;
return x;
}
int main()
{
int x = 3;
cout << "Value of X before modifying: " << x << endl;
x = fun(x);
cout << "Value of X after modifying: " << x << endl;
return 0;
}
|
Output
Value of X before modifying: 3
Value of X after modifying: 4
Time complexity: O(1).
Space complexity: O(1).
In the above example, if you want to change the value of a variable through a function using call by value then it is only changed in that function only and the actual value of the variable where it is defined is not changed. But, if we want to use the value from the function return the value to the main function.
Similar Reads
C++ Function Call By Pointer
Several ways exist in which data (or variables) could be sent as an argument to a function. Two of the common ones are Passing by Value and Passing by Reference. Example: C/C++ Code // C++ Program to demonstrate // Pass by value and // Pass by reference #include <iostream> using namespace std;
3 min read
Function Call by Value in Objective-C
In Objective-C, "call by value" refers to the practice of passing a copy of a value, rather than a reference to the original value, as an argument to a function or method. This means that any changes made to the value within the function or method do not affect the original value outside of the func
3 min read
A C/C++ Function Call Puzzle
Consider the following program. Predict the output of it when compiled with C and C++ compilers. C/C++ Code #include<stdio.h> void func() { /* definition */ } int main() { func(); func(2); } C/C++ Code #include <iostream> using namespace std; void func() { /* definition */ } int main() {
1 min read
JavaScript Function Call
The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). This allows borrowing methods from other objects, executing them within a different context, overriding the default value, and passing arguments. Syntax: cal
2 min read
Function Call by Reference in Objective-C
Just like other languages in objective-C also a function is used to perform some specific task. In objective-C, we can call a function by value or by reference. So, in this article, we will talk about the call by reference in Objective-C. The call-by-reference is a process of passing arguments to a
3 min read
clock() function in C
The clock() function in C returns the approximate processor time that is consumed by the program which is the number of clock ticks used by the program since the program started. The clock() time depends upon how the operating system allocates resources to the process that's why clock() time may be
2 min read
fma() function in C++
The fma() function takes three arguments a, b and c, and returns a*b+c without losing precision. The fma() function is defined in the cmath header file. If any argument passed to fma() is long double, the return type is long double. If not, the return type is double. Syntax: double fma(double a, dou
2 min read
Virtual Function in C++
A virtual function (also known as virtual methods) is a member function that is declared within a base class and is re-defined (overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object a
6 min read
atexit() function in C/C++
The function pointed by atexit() is automatically called without arguments when the program terminates normally. In case more than one function has been specified by different calls to the atexit() function, all are executed in the order of a stack (i.e. the last function specified is the first to b
3 min read
C++ Functions - Pass By Reference
Several ways exist in which data (or variables) could be sent as an argument to a function. Two of the common ones are Passing by Value and Passing by Reference. Passing by reference allows a function to modify a variable without creating a copy. We have to declare reference variables. The memory lo
5 min read