C++ Function Call By Value

Last Updated : 17 Jun, 2026

Call by Value is the default parameter-passing mechanism in C++, where a copy of the actual argument is passed to the function. Since the function works on a separate copy, changes made inside the function do not affect the original variable.

  • Original data remains safe from unintended modifications.
  • Suitable for passing small data types such as int, char, and float.
Function in C++
Function in C++

Working of Call by Value

When a function is called using call by value, the value of the argument is copied into the function parameter. The function operates on this copy, so any modifications remain local to the function and do not affect the original variable.

  • The argument's value is copied to the function parameter.
  • The function works on the copied value.
  • Changes made inside the function remain local.
  • The original variable remains unchanged after the function call.

Swapping Values Using Call by Value

The following program shows that swapping values inside a function does not affect the original variables when arguments are passed by value.

C++
#include <iostream>
using namespace std;

void swapValues(int x, int y)
{
    int temp = x;
    x = y;
    y = temp;

    cout << "Inside Function: ";
    cout << "x = " << x << ", y = " << y << endl;
}

int main()
{
    int x = 1, y = 2;

    cout << "Before Function Call: ";
    cout << "x = " << x << ", y = " << y << endl;

    swapValues(x, y);

    cout << "After Function Call: ";
    cout << "x = " << x << ", y = " << y << endl;

    return 0;
}

Output
Before Function Call: x = 1, y = 2
Inside Function: x = 2, y = 1
After Function Call: x = 1, y = 2

Explanation: The function receives copies of x and y. The swap operation affects only these copies, so the original variables in main() remain unchanged.

Modifying a Value Using Call by Value

Although the original variable cannot be modified directly, the function can return the modified value and store it back in the variable.

C++
#include <iostream>
using namespace std;

int increment(int x)
{
    x++;
    return x;
}

int main()
{
    int x = 3;

    cout << "Before Function Call: " << x << endl;

    x = increment(x);

    cout << "After Function Call: " << x << endl;

    return 0;
}

Output
Before Function Call: 3
After Function Call: 4

Time complexity: O(1).
Space complexity: O(1).

Explanation: The function modifies a copy of x and returns the updated value. The returned value is then assigned back to the original variable.

Advantages of Call by Value

Call by value offers safety and simplicity by ensuring that the original data remains unchanged during function execution.

  • Prevents accidental modification of original data.
  • Simple and easy to understand.
  • Suitable for small data types such as int, char, and float.
  • Makes functions independent of external variables.

Limitations of Call by Value

Since a copy of the argument is created for every function call, call by value may introduce additional memory and performance overhead.

  • Creates a copy of the argument, which may increase memory usage.
  • Passing large objects such as arrays, strings, or vectors can be expensive.
  • Cannot directly modify the caller's variables.
  • Less efficient for recursive or performance-critical applications involving large data.

Related article:Difference Between Call by Value and Call by Reference in C++

Comment