Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

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; 
    }

     

error: Content is protected !!