Introduction to C++ pass by reference
In C++, pass by reference is defined as referring to the address of the values that are passed as the arguments to the function, which means passing the address of the values to the functions are not the actual values this method of passing such values is known as pass by reference and method where we are passing the actual values is pass by value. In general, pass by reference is also known as pass by the address of the values declared in the arguments in the calling function relative to the formal arguments of the called function, which can modify the values of the parameters by using this pass by reference which is using the address of the values.
Working of pass by reference in C++
In this article, we will see a pass by reference in C++. In C++, we have seen simple variables passed as arguments to the functions, so similarly, we can also pass an address of the arguments or pass a reference as arguments to a function in C++ using this pass by reference concept and is very important when in the program we require to change the value of the arguments where it cannot be done bypass by value.
The pass by reference is usually understood if we have known about pointers in C++; therefore, pass by reference is simply defined as the passing of the address of the values in the arguments, which are reference arguments that are initialized with actual parameters whenever the function is called by the calling function, and the called function can modify these pass by reference values, but we cannot modify the values when we use pass by values in the arguments of the called function.
Examples of pass by reference in C++
Given below are the examples of pass by reference in C++:
using namespace std;
void callee_func(int &i)
i = i+2;
int i = 0;
cout << "Value of i before it is called by the caller function is :" << i << endl;
cout << "Value of i now is :" << i << endl;
In the above program, first, we have defined a function which is a callee function, and it can have a normal variable as arguments then they are formal arguments, and here it is “int i”, but here in the above example, we are passing a reference which means we are passing the address of the variable “i” and to get the address in C++ we declare it as “&i”. In the callee_func, we have a very simple calculation we are storing i value incremented twice “i = i +2”. Then we define the main function, which is considered a caller function as we call the callee_func inside the main function.
In the main() function, we are initializing the value of i = 0 and then calling the callee_func(), which we have passed a reference of I, and we can see both caller and callee_func are pointing to the variable i as we have passed a reference of i the value of i is changed or modified from 0 to 2 after calling the callee_func(). This way, in the above screenshot, we can see the output of the above program. In this way, we can see no new copy of i is made, so the overhead of copying can be saved, which is very efficient in creating programs when we are passing objects of larger classes.
In C++, we have the const keyword, which is used for setting constant values that means we cannot change the value of a variable. So if we are using the pass by reference and we do not want to change or modify the value of the variable passed in the argument, then we can use this “const” keyword before declaring the variable’s datatype, which will not modify the values of the variable which is passed by reference in the callee function.
using namespace std;
void callee_func(const int& p, int& q)
//p = p * 2;
q = q + 1;
int p = 4, q = 4;
cout << "Value of p before it is called by the caller function is :" << p << endl;
cout << "Value of q before it is called by the caller function is :" << q << endl;
cout << "Value of p now is :" << p << endl;
cout << "Value of q now is :" << q << endl;
In the above program, we can see in the callee_func() we have declared two variables, one with using the “const” keyword for the reference such as “const int &p” and other “&q”. So we are trying to print the values of variables p and q by passing the reference of these variables in the argument where it will modify the values. Still, if we use the “const” keyword, we cannot modify so when so in the above program we can the variable “p” is made constant, so we cannot access the changed value of the reference. Hence, we are printing the values before and after using the pass by reference in the argument of the function. So when you are trying to access the value by the reference of “p”, which is made constant will give an error as the value cannot be changed.
In this article, we conclude that pass by reference in C++ is defined as passing the address of the variables in the arguments of the callee function is known as pass by reference, which means we can modify the values even if the values are changed inside the callee function which these variables are same in both callee and caller function. In this article, we saw a simple example of how we can fetch the recent values of the variables passed as arguments using the address of the variables or pass by reference. In this article, we also saw if we don’t want the pass by reference to modify the value, which usually an illegal calling for such pass by reference with an example, and this may give us an error also if we are trying to refer to this const pass by reference variable.
This is a guide to C++ pass by reference. Here we discuss the introduction and working of pass by reference in C++ with examples, respectively. You may also have a look at the following articles to learn more –