Call By Value Function Calls in C++

In Functions, by default, C++ lets you pass variables by value (Call by Value). This is to make sure it is more difficult to inadvertently create bugs in the code. These bugs would come easy if the default was a “Call by Reference” instead (read more on Call by Reference).

Let’s explore this further; “Call by Value does not change the variable you pass to a function, even if that value is changed by the function. So this default setup for parameter passing makes it hard for you to unknowingly let the function change the variable that was passed. In other words, the function does not return a new value to the passed variable. So you can consider the function argument to be in local scope.

Basic Example of Call by Value

Let’s look at an example below to understand better:

void CallByVal(int a)
{
    a = a + 10;
}

int main()
{
    int num = 10;

    cout << "num before calling by value:" << num << "\n";

    CallByVal(num);

    cout << "num after calling by value:" << num;
}

Output:

Here you see the results. The variable “num” has not been changed by the function “CallByVal“. Even though the function used the value 10 in the argument ‘a‘ and changed it to 20, but did not return it. So the value of 10 is still inside the variable “num“. In other words, “num” has not been modified by the function. You can see in the program output, the before and after from the function call, is still the value of 10.

Return Altered Value from Functions

Ok, so what can you do inside a “call by value” function anyway?

In the above example you can only work on the value passed to argument ‘a‘ inside the function, say to make calculations to change private or global program variables not affected by “Call by Value” or pass back a value using the “return” statement.

With this in mind, let’s change the program to include a static global variable and a calculation inside two functions called BeforeReentry() and AfterReentry() returning a float value.

In this program we will calculate the speed of a space ship in earth orbit (17000 feet per / sec). The static global variable is kilometer (3000 feet per / sec) of type int.

We will still use ‘a‘ as the argument of the function, but of type float. And also make the function return the calculation (use float instead of void). So the function will return a value of type float.

In the main function, we will set “speed_of_space_ship” variable of type float with a value of (17000 feet per second). Then the two functions will be called and printed to the console. Then the value of speed_of_space_ship will be printed as well.

#include <iostream>
using namespace std;

int const kilometer = 3000; //3000 feet ... global variable.

float BeforeReentry(float a)
{
    // 17000 / 3000 = 5.66667
    return a / kilometer;
}

float AfterReentry(float a)
{
    a = a - 10000; //now speed is 7000 feet per sec.

    // 10000 / 3000 = 2.33333
    return a / kilometer;
}

int main()
{
    float speed_of_space_ship = 17000; //17000 feet per second

    cout << "speed of ship before reentry: " << BeforeReentry(speed_of_space_ship) << " kilometers per second" << "\n\n";

    cout << "speed of ship after reentry: " << AfterReentry(speed_of_space_ship) << " kilometers per second" << "\n\n";
	
    cout << "speed_of_space_ship variable after calling both functions: " << speed_of_space_ship << "\n\n";

    return 0;
}

So you see the variable “speed_of_space_ship” in the main function still does not change after both function calls even though it was changed inside the second function to 7000 feet per sec.

We used the two functions to do what functions do well… return a value. In this case, the speed of the ship was calculated to be 5.6 kilometers per second before reentry, and 2.3 kilometers per second after reentry.

But inside the functions we could have calculated to change non static global variables or use void functions to print text to the console screen. But global variables are usually not good unless you make them “const” to prevent the values from changing during the program execution. It’s just a good programming practice in C++.

There you have it for Call by Value function calls.

You might want to continue and read…

C++ Part 8 – Functions

C++ Part 7 – Pointers

C++ Part 6 – Arrays & Strings

C++ Part 5 – “switch and loops”

C++ Part 4 – else if Statement in C++

C++ Part 3 – Conditional “if” Statement

C++ Part 2 – The Basics of C++

C++ Part 1 – Installing Visual C++ Express

Share this tutorial with your friends:

Leave a Comment

Your email address will not be published. Required fields are marked *