In the last post, I have explained the difference between the pointers and references in C++. Also, we have already seen the advantage of references over the pointers.
Here In this article, we will learn how to return by reference in C++ in a function and use it efficiently in your code. I assume that you are already familiar with the function.
You can check related Blog Post
In C++ pointer and reference, both are used to access the object indirectly. Also, we can pass both in function and returns from the function.
The basic function signature of return by reference:
data_Type& function_name(parameters) { //function body }
where,
data_Type is the return type of the function, and parameters are the list of parameters that passed-in function.
You can also use CV qualifier while returning a reference from the function, see the below function signature.
data_Type const& function_name(parameters) { //function body }
Now let’s see an example code to understand how to return a reference from a function in C++.
#include <iostream> using namespace std; //global variable int gData = 20; // Function returning reference int& fun() { // Print the address cout << "gData = " << gData << " The address of gData is " << &gData <<"\n" <<endl; // Return reference return gData; } int main() { // Since the function returns reference // of gData,so we can update the value of gData fun() = 10; // Print gData and its address cout << "data = " << gData << " The address of data is " << &gData <<"\n" << endl; return 0; }
Output:
In the above program ,function fun() return type is int&. Hence, this function returns a reference of the variable gData.
Returning reference from function means that the function is returning variable not its value. So function fun is returning gData and you can be assigned a value as done in statement fun() = 10;
Note: Reference which returning from function must be valid. I will explain mentioned notes in the below statements.
If you already know how to program and just want to learn C++, you can start with the C++ Fundamentals course. The good thing is that TRIAL IS FREE
Duration: Approx. 43 hours
Rating: 4.6
As I have explained in the beginning that you can also use a CV qualifier while returning a reference from the function. So if you will use const while returning the reference you not able to change the value and if you will try you get the compiler error. Let’s see an example code,
#include <iostream> using namespace std; //global variable int gData = 20; // Function returning reference int const& fun() { // Print the address cout << "gData = " << gData << " The address of gData is " << &gData <<"\n" <<endl; // Return reference return gData; } int main() { fun() = 10; //error because const return 0; }
Output:
Important points you should remember When Returning by Reference in C++
1. Return reference only when required.
2. You must not return a local variable as a reference from a function.
int & fun() { int data = 10; return data; }
The behavior of the function will be undefined because the life of the data variable only within the function.
3. We can return built-in and user-defined types as a reference from the function.
4. If the function returning a reference, then we can not return a constant value from it.
int & fun() { return 10; //error }