A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/cpp-function-call-by-value/ below:

C++ Function Call By Value

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:

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:

Example 1: C++
// C++ Program to implement
// Swapping using Call by function
#include <iostream>
using namespace std;

// Swap function to demonstrate
// call by value method
void swap(int x, int y)
{
    int t = x;
    x = y;
    y = t;
    cout << "After Swapping in function x: " << x
         << ", y: " << y << endl;
}

// Driver Code
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++
// C++ Program to demonstrate
// Call by value
#include <iostream>
using namespace std;

// Function to change value of x
int fun(int x)
{
    x += 1;
    return x;
}

// Driver Code
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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4