Return by Reference in C++

Return by Reference in C++

Return by reference in C++ is very different from Call by reference. Functions act as a very important role when variable  are returned as reference. By the help of references  C++ program can be made easier to read and maintain.

Function signature of Return by Reference

dataType& functionName(parameters);

 

Program of return by reference in C++

#include <iostream>
using namespace std;
  

int x;
  
// Function returns as a return by reference
int& retByRef()
{
    return x;
}
  
int main()
{
    // Function Call for return by reference
    retByRef() = 10;
    cout << x;
    return 0;

Output

10

 

Read more, Recursion in C++

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *