‘Call By Value’ Function Calls in C++

Call by Value:

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 indeed changed by the function. So the default setup for parameter passing to functions makes it hard for you, the programmer, to unknowingly let the function change the variable that was passed.

Let’s look at an example below:


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.

 

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 global variables not affected by “Call by Value” or pass back a value using the “return” statement.

With this in mind, let’s alter the program to include a global variable and a calculation inside function CallByVal.

In this program we will calculate the speed of a bullet (1000 feet per/sec). The global variable is “kilometer” (3000 feet).

We will still use ‘a‘ as the argument of the function, but also make the function return the calculation (use int instead of void).

In the main function, we will replace “num” with “speed_of_bullet” (1000 feet per second).


#include <iostream>

using namespace std;

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

int CallByVal(int a)

{

	a = a + 500; //now speed is 1500 feet per sec.

	return kilometer / a;

}

int main() {

	int speed_of_bullet = 1000; //1000 feet per second

	cout <<"speed_of_bullet variable before calling by value: " <<speed_of_bullet<<"\n\n";

	cout <<"speed of bullet is: " << CallByVal(speed_of_bullet) << " kilometers per second" <<"\n\n";

	cout <<"speed_of_bullet variable after calling by value: " <<speed_of_bullet;

	getchar(); // Just To Prevent Screen from Disappearing

	cin.get(); // Just To Prevent Screen from Disappearing

	return 0;

}

So you see the variable “speed_of_bullet” still does not change after the function call even though it was changed inside the function to 1500.

We used the function to do what a function usually does … return a value. In this case, the speed of the bullet was calculated to be 2 kilometers per second.

But inside the function we could have calculated other things and put those values in global variables. 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.

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

 

More C / C++ and Dev Programming Tutorials:

Share this tutorial with your circle of friends:

Leave a Reply

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