Call C++ Functions by Reference or Pointers

C++ Call By Reference:

In C++ programming, parameters of a function can be called in two ways, by value or by reference. Read more on Call by Value.

When the function is called by value, a copy of the parameters passed through a function call is created and that copy is used as the argument of the function. The actual value of the parameters is not changed if the corresponding arguments are modified inside the function body.

On the other hand when a function is called by reference, the actual value of the variable is not passed, but instead, the reference of the variables is passed to the function. Hence when the function modifies the value, the initial value which was passed through the function call also changes.

We can achieve this in two ways. We can simply “call by reference” or use “pointers”.

Note that in C++ “call by reference” is standard using the ‘ & ‘ symbol. In the older “C language”, you had to use “pointers” instead.

The following two examples helps to explain these concepts.

 

Call By Reference

Note the ‘&’ symbol is needed highlighted below before the variable. Copy and paste the code into your C++ editor.


#include <iostream>

using namespace std;

void CallByRef(int & a)

{

	a=20;

}

int main()

{

	int num =10;

	cout <<"Number before calling by reference is:" <<num<<"\n";

	CallByRef(num);

	cout <<"Number after calling by reference is:"<<num;

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

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

}

In this program, the value of variable num is 10. We have printed this value. After that we passed this variable as reference to the function CallByRef. The function modified the value of variable ‘a’ which actually holds this value to the passed to argument ‘num’. Hence when the value of ‘a’ is changed inside the function ‘CallByRef’ the value of ‘num’ is also changed, this now contains 20. The value of ‘num’ is again printed after calling the function.

Output:

 

Call By Reference Using Pointers

In C++ functions, arguments can also be passed by reference using “pointers” which has been explained in the following program example (Note the ‘ * ‘ and ‘ & ‘ symbols in the code example below.

Example 2: (copy and paste the code)


#include <iostream>

using namespace std;

void CallByRefPointer(int * a)

{

	* a=20;

}

int main()

{

	int num =10;

	cout <<"Number before calling by reference is:" <<num<<"\n";

	CallByRefPointer(&num);

	cout <<"Number after calling by reference is:"<<num;

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

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

}

In this example, the pointer has been used to call the function by reference. We know that pointers can hold a reference to a variable. In the above program we have passed a pointer which refers to the argument ‘num’, hence when the value of the pointer ‘a’ changes the corresponding variable ‘num’ to which it points also changes. The text output is the same as in program example 1.

Output:

 

One advantage of calling by reference is that it can modify the argument in the called function (if thats what you want) using the reference of those arguments which is passed to the function. However this is not the only advantage of passing the function by reference.

Sometimes you might want to pass the reference instead of the whole value due to efficiency reasons. For example, if you are passing a large object or a structure, it is convenient to pass the reference of these variables instead of passing the whole object or structure, which can be too large.

 

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 *