Return By Value, Return By Pointer, Return By Reference

 Return By Value, Return By Pointer, Return By Reference


Return by value:
In this the function returns some kind of value like the variable, it is the most simplest way to return the value. remember it will return a copy of the variable, and it also doesn't affect the scope of the variable.

int * myFunction() {
   .
   .
   .
}
Example:

int doubleValue(int x)
{
    int value = x * 2;
    return value; // A copy of value will be returned here
}
Return by Pointer/Address:
return by pointer also known as return by address. in this type the address of the variable is returned. return by address can only return the address of a variable, not a literal or an expression (which don’t have addresses). if you try to return the address of a variable local to the function, your program will exhibit undefined behavior. Consider the following example:

int* doubleValue(int x)
{
    int value = x * 2;
    return &value; // return value by address here
}
in the above example the scope of the value is inside the doubleValue function only and when the program will execute this function the variable value will be destroyed and it will be out of scope so the function will return the address at which the value is not their (dangling pointer).

Return by address was often used to return dynamically allocated memory to the caller:

int* allocateArray(int size)
{
    return new int[size];
}
 
int main()
{
    int *array = allocateArray(25);
 
    // do stuff with array
 
    delete[] array;
    return 0;
}
Return by Reference:
When a variable is returned by reference, a reference to the variable is passed back to the caller. The caller can then use this reference to continue modifying the variable, which can be useful at times.
However, just like return by address, you should not return local variables by reference. Consider the following example:

int& doubleValue(int x)
{
    int value = x * 2;
    return value; // return a reference to value here
}
same as the return by pointer, value which is an local variable will be out of of scope with the function return, which will end up with some warning or error.
Return by reference is typically used to return arguments passed by reference to the function back to the caller. In the following example, we return (by reference) an element of an array that was passed to our function by reference:

#include<iostream>
using namespace std;
int& tonLarge(int &,int &);
int main(){
    int x,y;
    cout<<"Enter first number : ";
    cin>>x;
    cout<<"Enter second number : ";
    cin>>y;
    tonLarge(x,y)=100;
    cout<<"First number is : "<<x;
    cout<<"\nSecond number is : "<<y;
    cout<<"\nSo larger number is set to:"<<tonLarge(x,y);
    return 0;
}
int& tonLarge(int &a, int &b){
    if(a>b)
        return a;
    else
        return b;
} // NOTE:Since the return type of tonLarge () is int &, the function returns
     reference to a or b (and notthe values).
Since the return type of tonLarge(int &a, int &b) is int&, the function returns reference to a or b depending on their values. this means that this function call can appear on the left-hand side of an assignment statement.

Comments

Popular posts from this blog

Datatypes In C++

Implicit And Explicit Type Conversion

Operators in C++