Search

7.4a — Returning values by value, reference, and address

In the three previous lessons, you learned about passing arguments to functions by value, reference, and address. In this section, we’ll consider the issue of returning values back to the caller via all three methods.

As it turns out, returning values from a function to its caller by value, address, or reference works almost exactly the same way as passing parameters to a function does. All of the same upsides and downsides for each method are present. The primary difference between the two is simply that the direction of data flow is reversed. However, there is one more added bit of complexity -- because local variables in a function go out of scope when the function returns, we need to consider the effect of this on each return type.

(Author’s note: This lesson has a funny lesson number because it was originally omitted from chapter 7)

Return by value

Return by value is the simplest and safest return type to use. When a value is returned by value, a copy of that value is returned to the caller. As with pass by value, you can return by value literals (eg. 5), variables (eg. x), or expressions (eg. x+1), which makes return by value very flexible.

Another advantage of return by value is that you can return variables (or expressions) that involve local variables declared within the function. Because the variables are evaluated before the function goes out of scope, and a copy of the value is returned to the caller, there are no problems when the variable goes out of scope at the end of the function.

Return by value is the most appropriate when returning variables that were declared inside the function, or for returning function arguments that were passed by value. However, like pass by value, return by value is slow for structs and large classes.

Return by reference

Just like with pass by reference, values returned by reference must be variables (you can not return a reference to a literal or an expression). 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. Return by reference is also fast, which can be useful when returning structs and classes.

However, returning by reference has one additional downside that pass by reference doesn’t -- you can not return local variables to the function by reference. Consider the following example:

See the problem here? The function is trying to return a reference to a value that is going to go out of scope when the function returns. This would mean the caller receives a reference to garbage. Fortunately, your compiler will give you an error if you try to do this.

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:

This prints:

5

When we call Value(sMyArray, 10), Value() returns a reference to the element of the array inside sMyArray that has the index 10. main() then uses this reference to assign that element the value 5.

Although this is somewhat of a contrived example (because you could access sMyArray.anValue directly), once you learn about classes you will find a lot more uses for returning values by reference.

Return by address

Returning by address involves returning the address of a variable to the caller. Just like pass by address, return by address can only return the address of a variable, not a literal or an expression. Like return by reference, return by address is fast. However, as with return by reference, return by address can not return local variables:

As you can see here, nValue goes out of scope just after its address is returned to the caller. The end result is that the caller ends up with the address of non-allocated memory, which will cause lots of problems if used. This is one of the most common programming mistakes that new programmers make. Many newer compilers will give a warning (not an error) if the programmer tries to return a local variable by address -- however, there are quite a few ways to trick the compiler into letting you do something illegal without generating a warning, so the burden is on the programmer to ensure the address they are returning will be to a valid variable after the function returns.

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

Conclusion

Most of the time, return by value will be sufficient for your needs. It’s also the most flexible and safest way to return information to the caller. However, return by reference or address can also be useful, particularly when working with dynamically allocated classes or structs. When using return by reference or address, make sure you are not returning a reference to, or the address of, a variable that will go out of scope when the function returns!

7.5 -- Inline functions
Index
7.4 -- Passing arguments by address

69 comments to 7.4a — Returning values by value, reference, and address

  • MrFinn

    PS. The latter example in the text works for this same reason, the struct is declared in gobal scope and therefore still keeps on existing after the function call.

  • MrFinn

    I found this sentence (from MSVC++ documentation) to be very clarifying:

    "A reference holds the address of an object, but behaves syntactically like an object."

    Therefore, the object which is passed back to the caller must keep on existing (and it's address as well) when the function goes out of scope. This is why you can not pass reference to a local variable which ceases to exist. If you change the orginal sample code so that nValue is declared in global scope it will work because nValue object stays alive.

    int nValue; // declared in global scope.

    int& DoubleValue(int nX) {
    //int nValue = nX * 2;
    nValue = nX * 2;
    return nValue;
    }

    BTW. Sunnys program does return the same error message here (Qt & MinGW) and the math is wrong:
    "warning: reference to local variable 'nValue' returned [enabled by default]"

  • nimingzhe2008

    I have the same question as Sunny Chouhan.

  • Sunny Chouhan

    Hi Alex,
    As Mention in your article 'Return by reference' local variable cannot be return because they go out of scope when function returns.
    But I did not receive any compile error
    program is running fine
    I am using visual studio 2010 (vc++)

    #include
    using namespace std;

    int main()
    {
    int &DoubleValue(int x);
    int number;
    number = DoubleValue(10);
    cout << "Number = " << number << endl;
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    return 0;
    }
    int& DoubleValue(int nX)
    {
    int nValue = nX * 2;
    return nValue; // return a reference to nValue here
    }

  • Hey Alex,

    great tutorial! It's so amazing simple to learn C with your help, I've never thought before.

    I've found a little error he links aren’t correct:

    « 7.14 — Ellipses (and why to avoid them)  
      13.1 — Input and output (I/O) streams »
  • vamsi
    Alex,
    
    Can you start STL tutorials
    
  • ellankavi

    Hello!
    In the following code, is the dynamic array deleted properly?

    #include<iostream>
    
    int* CreateArray(int nSize)
    {
    	int* const npPtr = new int[nSize];
    
    	//initialize the array
    	for (int iii=0;iii<nSize;iii++)
    		npPtr[iii]=iii;
    	return npPtr;
    }
    
    int main()
    {
    	int nSize=5;
    	int *apArray=CreateArray(nSize);
    	delete[] apArray;
    	system("pause");
    	return 0;
    }
    

    Thanks :)

    • abcd

      My guess is, since apArray was not created using

      new int[]

      but rather something like

      int* apArray = int* npPtr

      it is most likely that the compiler does not take it as an Array.
      Try this:

      delete apArray

      Then access to any element will result in garbage

      std::cout <<  apArray[0];

      Once again, this is my guess. Hopefully Alex or someone else can verify this.

      BTW, great tutorial Alex! Thanks a ton.

  • haya

    suppose i have to write one function to give me the highest and lowest and average how i will write it without using void?
    by using refrence but how?

  • pankaj

    I try to return the value of the local variable by using return by reference. It is possible to do that.
    I am getting the correct values.
    Compiler is only giving waring...warning C4172: returning address of local variable or temporary

    plese see the code below and kindly give your comments..

    
    
    result of below code is 
    
    Hello World!
    res = 7 
    res1 = 12
    
    int add (int x, int y)
    {
    int result = x+y;
    return result;
    }
    
    // return by reference 
    int& multiply(int x, int y)
    {
     int result = x*y;
     return result;
    }
    
    int main(int argc, char* argv[])
    {
    	printf("Hello World!\n");
    	int res = add(3,4);
    	std::cout<< "res = " << res << std::endl; 
    
    	int res1 = multiply(3,4);
    	std::cout << "res1 = " << res1 << std::endl; 
    	return 0;
    }
    
    
    • Anna
      But you can not be sure of how long that value exists and how 
      long you can use that variable. Actually when you are 
      returning from that function, the scope of that variable does 
      not exist, but in memory there exists such an address and a 
      value since nobody changes it. but there is a chance that 
      when a new memory allocation is done by our process, the OS 
      takes this memory and gives it.that time the value at this 
      particular address might have changed and we may not be 
      getting what we are expecting. in very simple terms using 
      such a variable is just unsafe like using a property that we 
      already sold. when the new owner comes you have to give it 
      to that person and stop using it. but you never know when.
      such usage is very unsafe with big programs having multiple
      threads
      
      • mccp13

        what does the int& mean in pankaj's function int& multiply(int, int)..
        does it mean it returns the address of an integer or a reference to an integer..
        and how did the local variable res1 int his main function accept the return type if the function?

        Nice Tutorial btw.. i'm using this to learn c++ :)

  • How does a varible passed by reference, change the original variable's data while being modified at once? (I know this sounds confusing but please try to understand :)

    • Kurt

      A reference works like an alias for the original data. You're not changing the value of the reference itself, because references are in a sense immutable. You're changing the value of the data the reference aliases. It's like modifying the dereferenced value of a pointer.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">