C++

C++ Call By Address and Call By Reference

C++ is a flexible general-purpose programming language. It was originally created by Bjarne Stroustrup, a Danish computer scientist, back in 1985. C++ supports three-parameter passing methods, i.e., call by value, call by address, and call by reference. In this article, we are going to discuss about call by address and call by reference mechanism.

What is a function?

Before we jump into the actual topic, we need to understand what the function is in C++. Many of you may already be familiar with functions.

A function is basically a piece of code that can be used to perform a certain task. A function is mainly used to reduce the repetitive code in a C++ program. It takes input as parameters and returns the output as a return value. If we define the function once, we can call/use it multiple times in the later part of our program. That way, we save a lot of repetitive code in the program.

Every C++ program shall have the “main()” function. The “main()” function is the entry point for a C++ program. Apart from the “main()” function, the programmer can define as many functions as they want.

Here is the syntax of defining a function:

Return_type Function_Name (Input parameter List)

Function in C++ can accept 0 or more number of input parameters, whereas it can return only one return-value.

What is Address?

There are two types of variables in C++ (similar to C language) – Data Variable and Address Variable. The address variable is used to store the address of another data variable. For example, let’s consider the following code snippet:

int i = 100;
int *ptr = &i;

Here, the first statement tells us that the variable “i” is a data variable, and it is storing the value 100. In the second statement, we are declaring a pointer variable, i.e. “ptr,” and initializing it with the address of the variable “i”.

What is Reference?

The reference is another powerful feature of C++ language. Let’s consider the following code snippet:

int a = 200;
int &r = a;

In this example, we have declared an integer, i.e. “a” and then declared a reference variable “r”, which is initialized with the value of “a”. So, the reference variable is nothing but an alias of another variable.

Parameter passing methods:

There are three types of parameter passing methods in C++ language:

  1. Call by value / Pass by value
  2. Call by address / Pass by address
  3. Call by reference / Pass by reference

In this article, we are discussing about the – Call by address and Call by reference.

What is Call By Address / Pass by address?

In the case of the Call by address / Pass by address method, the function arguments are passed as address. The caller function passes the address of the parameters. Pointer variables are used in the function definition. With the help of the Call by address method, the function can access the actual parameters and modify them. We will see an example of the Call by address method later section of this article.

What is Call By Reference / Pass by reference?

In the Call by reference / Pass by reference method, the function parameters are passed as a reference. Inside the function definition, the actual parameters are accessed using the reference variable.

Examples:

Now, since we understand the concept of parameter passing methods, we will see several example programs to understand the parameter passing mechanism in C++:

  1. Example-1 – Call by Address (1)
  2. Example-2 – Call by Address (2)
  3. Example-3 – Call by Reference (1)
  4. Example-4 – Call by Reference (2)

The first two examples are given to explain how the Call by address method works in C++. The last two examples are to explain the Call by reference concept.

Example-1 – Call by Address (1)

In this example, we are going to demonstrate the call by address mechanism. From the “main()” function, we are calling the “hello()” function and passing the address of “var”. In the function definition, we are receiving the address of “var” in a pointer variable, i.e., “p”. Inside the function hello, the value of “var” is being changed to 200 with the help of the pointer. Therefore, the value of “var” is getting changed to 200 inside the “main()” function after the “hello()” function call.

#include <iostream>
using namespace std;

void hello(int *p)
{
    cout << endl << "Inside hello() function : " << endl;
    cout << "Value of *p = " << *p << endl;
    *p = 200;
    cout << "Value of *p = " << *p << endl;
    cout << "Exiting hello() function." << endl;
}

int main()
{
    int var = 100;
    cout << "Value of var inside main() function = " << var << endl;
   
    hello(&var);
   
    cout << endl << "Value of var inside main() function = " << var << endl;
   
    return 0;
}

Example-2 – Call by Address (2)

This is another example of the call by address method. In this example, we are going to explain how the call by address method can be used to solve a real-life problem. For example, we want to write a function to swap two variables. If we use the call by value mechanism to swap two variables, the actual variables do not get swapped in the caller function. The call by address method can be used in such a scenario. In this example, we are passing the address of both var_1 (&var_1) and var_2 (&var_2) to “mySwap()” function. Inside the “mySwap()” function, we are swapping the values of these two variables with the help of the pointers. As you can see in the below output, the actual value of these variables is getting swapped in the “main()” function after the “mySwap()” function is executed.

#include <iostream>
using namespace std;

void mySwap(int *vptr_1, int *vptr_2)
{
   int temp_var;
   temp_var = *vptr_1;
   *vptr_1 = *vptr_2;
   *vptr_2 = temp_var;
}

int main()
{
   int var_1 = 100;
   int var_2 = 300;

   cout << "Before calling mySwap() function, value of var_1 : " << var_1 << endl;
   cout << "Before calling mySwap() function, value of var_2 : " << var_2 << endl;
       
   cout << "Calling mySwap() function - Call by address." << endl;
   mySwap(&var_1, &var_2);

   cout << "After calling mySwap() function, value of var_1 : " << var_1 << endl;
   cout << "After calling mySwap() function, value of var_2 : " << var_2 << endl;
   
   return 0;
}

Example-3 – Call by Reference (1)

In this example, we are going to demonstrate how call by reference works in C++. In the “hello()” function definition, the value is being received as a reference variable (&p). With the help of the reference variable (i.e., p), we are able to change the value of the actual parameter (var) inside the “main()” function.

#include <iostream>
using namespace std;

void hello(int &p)
{
    cout << endl << "Inside hello() function : " << endl;
    cout << "Value of p = " << p << endl;
    p = 200;
    cout << "Value of p = " << p << endl;
    cout << "Exiting hello() function." << endl;
}

int main()
{
    int var = 100;
    cout << "Value of var inside main() function = " << var << endl;
   
    hello(var);
   
    cout << endl << "Value of var inside main() function = " << var << endl;
   
    return 0;
}

Example-4 – Call by Reference(2)

This is another example of a call by reference. In this example, we are going to demonstrate how call by reference works in C++ with the help of a real-world example. The “mySwap()” function is called from the “main()” function with the following parameters – var_1 and var_2. Inside the “mySwap()” function, we are receiving the parameters as reference variables.

#include <iostream>
using namespace std;

void mySwap(int &vref_1, int &vref_2)
{
   int temp_var;
   temp_var = vref_1;
   vref_1 = vref_2;
   vref_2 = temp_var;
}

int main()
{
  int var_1 = 100;
  int var_2 = 300;
   
  cout << "Before calling mySwap() function, value of var_1 : " << var_1 << endl;
  cout << "Before calling mySwap() function, value of var_2 : " << var_2 << endl;
       
  cout << "Calling mySwap() function - Call by reference." << endl;
  mySwap(var_1, var_2);
   
  cout << "After calling mySwap() function, value of var_1 : " << var_1 << endl;
  cout << "After calling mySwap() function, value of var_2 : " << var_2 << endl;
   
  return 0;
}

Conclusion

Understanding the parameter passing methods in C++ is very crucial. The C programming language supports the Call by value and Call by address only. But, C++ supports Call by reference along with the previous two mechanisms. In this article, we have seen several working examples to understand the concept of Call by address and Call by reference. Call by address is a very powerful and popular method in embedded domain applications.

About the author

Sukumar Paul

I am a passionate software engineer and blogger. I have done my Masters in Software Engineering from BITS PILANI University, India. I have very good experience in real-time software development and testing using C, C++, and Python. Follow me at thecloudstrap.com.