| by Arround The Web | No comments

Return Reference in C++

C++ provides a facility to return a value or an address by its reference rather than pointers. Using references instead of pointers can make a C++ program simpler to read and manage. In C++, references and pointers are closely related to one another. The primary distinction is that although references are merely an alternate name, “alias” for another variable, pointers can be used in operations such as adding values. A reference is an alternate name or duplicate of the original value and is denoted by the “&”symbol.

Example 1:

We import the “iostream” header file and then utilize the “std” namespace. The header files are imported in C++ codes as many functions are defined. Then, we create a return reference function by placing the “&” symbol with the function’s name, “returnTheValue”.

Here, the “value” reference is now inserted. Underneath this, we print the value and the address with the “&value” reference. Then, we place the return reference and place the “value”. Now, the “main()” is invoked here, and we initialize “n1” with the value of “44”. Below this, “int& n2” is initialized with the “returnTheValue(n1)”. Now, we print the value of “n1” as well as its address. Then, we print the value of “n2” as well as the address of “n2” by utilizing the “cout”.

Code 1:

#include <iostream>
using namespace std;
int& returnTheValue(int& value)
{
    cout << "Value = " << value << endl
        << " The address of value is "
        << &value << endl;
    return value;
}
int main()
{
    int n1 = 44;
    int& n2 = returnTheValue(n1);
    cout << "n1 = " << n1 << endl
        << " The address of n1 is "
        << &n1 << endl;
    cout << "n2 = " << n2 << endl
        << " The address of n2 is "
        << &n2 << endl;
    return 0;
}

 
Output:

Here, we can note that a reference is only an alternate name of another variable as shown in the following. As the address of value, “n1” and “n2” never change.

Example 2:

We use the “std” namespace after importing the “iostream” header file.  Next, we use the function name “MyReturnValueFunc” and the “&” symbol to build a return reference function. The reference to the “v1” variable is placed here. We print the value and the address with the “&v1” reference underneath this. Next, we insert the “return reference” by utilizing “return” and “v1” in this location. Here, “main()” is called, and “num_1” is initialized with the value of “19”. The initialization of “int& num_2” is done with “MyReturnValueFunc(num_1)”.

Currently, we print the value and address of “num_1” and, using “cout”, we print the value and address of “num_2”. We now change the value of “num_1” by utilizing the address that is returned here by “MyReturnValueFunc”. This function returns the alternate name of “v1” which is also the alternate name of “num_1”. So, we change its value and set it to “91”. We assign “91” to “MyReturnValueFunc(num_1)” which acts as the alias here. Then, we print the value again and the address of “num_1”.

Code 2:

#include <iostream>
using namespace std;
int& MyReturnValueFunc(int& v1)
{
    cout << "The value of v1 = " << v1 << endl
        << " The address of v1 variable is "
        << &v1 << endl;
    return v1;
}
int main()
{
    int num_1 = 19;
    int& num_2 = MyReturnValueFunc(num_1);
    cout << "The value of num_1 = " << num_1 << endl
        << " The address of num_1 is "
        << &num_1 << endl;
    cout << "The value of num_2 = " << num_2 << endl
        << " The address of num_2 is "
        << &num_2 << endl;
    MyReturnValueFunc(num_1) = 91;
    cout << "Now, the value of num_1 = " << num_1 << endl
         << "The address of num_1 is "
         << &num_1 << endl;
    return 0;
}

 
Output:

As demonstrated in the following, we can see that a reference is merely an alternative name for another variable since the address of the “v1”, “num_1”, and “num_2” values remained constant:

Example 3:

The “iostream” header file is imported and the “std” namespace is used. Since numerous functions are specified in the header files, we import them into the C++ codes. Here, we create a “ReturnRefFun()” function in which we place “int& my_ref” which returns the reference. The “int& ReturnRefFun” is declared here as the reference function. After this, we increment the value of the “my_ref” variable. Underneath this, we put “return” which returns the reference of “my_ref”.

After this, the “main()” method is invoked here. Then, we initialize the “first_value” variable with “21”. Below this, we return the copy of the reference by placing “first_value” in the “ReturnRefFun” function and save it in the “copied_value” variable. Then, we print both the “first_value” as well as the “copied_value” by utilizing the “cout”. Underneath this, we increment the “copied_value” variable by placing the “copied_value++”. Then, we print the “copied_value” after incrementing it and the “first_value” using “cout”. After this, we return the reference with the help of initializing the “int& ref_value” variable with “ReturnRefFun(first_value)”.

After this, we print the value of the “my_ref” variable which we copied. Then, we print the value of the “first_value” variable. Underneath this, we increment the value of “ref_value” by putting “ref_value++”. Below this, we print the incremented value of “ref_value” and also the “first_value” variable with the aid of “cout”. When the “ref_value” is changed, the “first_value” will also change.

Code 3:

#include <iostream>
using namespace std;
int& ReturnRefFun(int& my_ref){
    my_ref++;
    return my_ref;
}
int main() {
    int first_value= 21;
    int copied_value=ReturnRefFun(first_value);
    cout << "The first value is : " << first_value << endl;
     cout<< "The copied value is : " << copied_value << endl;
    copied_value++;
    cout<< "The copied_value is incremented: " << copied_value << endl;
    cout << "The first value : " << first_value << endl;
    int& ref_value=ReturnRefFun(first_value);
    cout << "The reference copied value: " << ref_value << endl;
    cout << "The first value : " << first_value << endl;
    ref_value++;
    cout << "The reference value is incremented : " << ref_value << endl;
    cout << "The first value : " << first_value << endl;
    return 0;
}

 
Output:

Here is the outcome of the previous code where we utilized the “return reference” technique. The example shows the distinction between returning a duplicate of the reference variable and returning the reference variable itself.

Example 4:

Here, “int& rByRef” is declared as the reference function which returns the reference variable. We pass the “int& data” to this “int& rByref()” function. Here, we print the address of the “data” variable and then utilize the return reference below this. Now, we initialize the “x_var” variable after invoking the “main()” method. Then, we print the address of “x_var” here by putting the “&x_var” in the “cout”.

Underneath this, we utilize the reference variable by assigning “rByref(x_var)” to the “int& y_var”. Then, we also print the address of that “&y_var” reference variable. Below this, we copy the “x_var” variable into the “z_var” variable and also print the address of this copied variable which is “&z_var”. After this, we call the “rByref()” function, pass the “x_var” variable as the parameter inside it, and assign “93” to this variable. We also render the address of the “x_var” again by putting the “&x_var” in the “cout”.

Code 4:

#include <iostream>
using namespace std;
int& rByref(int& data)
{
    cout<< "Address of data: "<< &data << endl;
    return data;
}
int main()
{
    int x_var = 42;
    cout<< "Address of x_var: "<< &x_var << endl;
    int& y_var = rByref(x_var);
    cout << "Address of y_var: "<< &y_var << endl;
    int z_var = rByref(x_var);
    cout << "Address of z_var: "<< &z_var << endl;
    rByref(x_var) = 93;
    cout<< "Address of x_var: "<< &x_var << endl;
    return 0;
}

 
Output:

The result makes it clear that the cloned variable’s address, “z_var”, differs from all the other locations that the original variable, “x_var”, references.

Conclusion

The “return reference” concept is explored in this tutorial in detail. We learned that the “return reference” is similar to the “pointers” in C++ programming. We discussed that to indicate which function returns a reference, the “&” symbol needs to be utilized with the function’s return type. We illustrated some examples and their outcomes and understood this concept in this tutorial.

Share Button

Source: linuxhint.com

Leave a Reply