. Advertisement .
..3..
. Advertisement .
..4..
Normally, we still prefer using return type as value because it is safer and easier to understand, but it has some limitations when working with large data types, dynamically allocated data. .. In this case, we will use the method that returns the reference value. To help you better understand, here we provide more information about “Return by Reference” in C++. Let’s start!
What is “Return by Reference”?
1. “Return by Reference” and example
In C++ programming, you can not only pass a value by reference to a function, but you can also return a value by reference.
Similar to passing arguments to a function as a reference, the return value when using this method must be a variable (cannot return a specific value or an expression).
When a return value is a reference, a reference is created and returned to the function call. We can use the returned reference to continue changing the data within the referenced memory.
The important thing to remember when returning by reference is that a normal function will return a value, but a function of this type will not. So you cannot return a constant from this function.
Here is an example:
#include <iostream>
int n;
int& test();
int main() {
test() = 5;
cout<<n;
return 0;
}
int& test() {
return n;
}
Output:
5
According to the above example, int& is the return type when declaring the test() function. So this function returns the reference of the variable n.
The return statement is return n;. Unlike return value, this statement does not return the value of n, instead it returns the variable itself (the address).
So when the variable is returned, it can be assigned a value as it did with test() = 5;
This statement will store 5 in the variable n, which is then displayed on the screen.
Below is another example that Return by reference is shown:
// C++ program to demonstrate return
// by reference
#include <iostream>
using namespace std;
// Global variable
int x;
// Function returns as a return
// by reference
int& retByRef()
{
return x;
}
// Driver Code
int main()
{
// Function Call for return
// by reference
retByRef() = 10;
// Print X
cout << x;
return 0;
}
Output
10
The value 10 will be set to the variable x since the return type of the aforementioned function, retByRef(), is a reference to the variable.
2. When to use:
When returning function arguments passed by reference.
When returning an element from an array passed to the function.
When returning a type of complex structs or classes that are not destroyed when out of the function (passed into the function).
3. When not to use:
When returning variables declared inside a function (using returns by value).
When returning an array or pointer value (using return by address).
Why should we Return by Reference?
A reference type return declaration can be made for a function. This assertion is appropriate for some following reasons:
1. Given the size of the information being provided, returning a reference is more effective than returning a copy.
2. The function’s type must be a l-value.
3. When the function completes, the referred-to object will not be removed from its scope.
Large objects can be passed to functions by reference more efficiently, and large objects can likewise be returned from functions more efficiently. The reference-return technique does not require the object to be copied to a temporary location before being returned. When the function must evaluate to a l-value, reference-return types can be helpful as well. This describes the majority of overworked operators, especially the assignment operator. In Overloaded Operators, overload operators are discussed.
Conclusion
Through this lesson, you’ve learned about the “Return by Reference” in C++ and when to and shouldn’t use it. Thank you for following the article. Please leave your comments or suggestions to develop the article better.
Read more
→ Undefined Reference To Pthread_create: Explanation And How To Deal With
Leave a comment