Loading...
C++ Return by Reference

C++ Return by Reference

In this article, you'll learn about how to return a value by reference in a function and use it efficiently in your program.


Return by Reference

  • In C++, Pointers and References held close relation with in another.
  • The major difference is that the pointers can be operated on like adding values whereas references are just an alias for another variable.
    • Functions in C++ can return a reference as it is return a pointer.
    • When function returns a reference it means it returns a implicit pointer.
  • In C++ Programming, not only can you pass values by reference to a function but you can also return a value by reference.
  • Return by reference is very different from Call by reference.
  • Functions behaves a very important role when variable or pointers are returned as reference.

Syntax

dataType& functionName(parameters);
where,
dataType is the return type of the function,
and parameters are the passed arguments to it.

Example 1: Return by Reference by using Global Variable

#include <iostream>
using namespace std;

// Global variable
int number;

// Function declaration
int& retByRef(){
    return number;
}

int main(){
    // Function call for return by reference
    retByRef() = 2;
    
    // print number
    cout << number;
    return 0;
}

Output

2

Working:
In program above, the return type of function retByRef() is int&. Hence, this function returns a reference of the variable number.

The return statement is return number;. Unlike return by value, this statement doesn't return value of number, instead it returns the variable itself (address).

So, when the variable is returned, it can be assigned a value as done in retByRef() = 2;. This stores 2 to the variable number, which is displayed onto the screen.


Example 2: Return by Reference by using Local Variable

#include <iostream>
using namespace std;

// Function to return as return by reference
int& retByRef(int& n){
    // print the address
    
    cout << "n = " << n << endl;
    cout << "The address of n is - " << &n << endl;
    
    // return by reference
    return n;
}

int main(){

    int a = 10;
    int& b = retByRef(a);

    // display 'a' and its address
    cout << "a = " << a << endl;
    cout << "The address of a is - " << &a << endl;

    // display 'b' and its address
    cout << "b = " << b << endl;
    cout << "The address of b is - " << &b << endl;
    
    // update the value of 'a'
    retByRef(a) = 12;

    // display 'a' and its address
    cout << "a = " << a << endl;
    cout << "The address of a is - " << &a << endl;
    
    return 0;
}

Output

n = 10
The address of n is - 0x7ffc24d15cdc
a = 10
The address of a is - 0x7ffc24d15cdc
b = 10
The address of b is - 0x7ffc24d15cdc
n = 10
The address of n is - 0x7ffc24d15cdc
a = 12
The address of a is - 0x7ffc24d15cdc

Things to Remember

  • Ordinary function returns value but this function doesn't. Hence, you cannot return a constant from the function.
    int& retByRef() {
    return 2;
    }
  • You cannot return a local variable from this function.
    int& retByRef() {
        int num = 2; 
        return num; 
    }

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Return by reference in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Arrays.

- Related Topics