Thread: returning by reference / value.

  1. #1
    Registered User
    Join Date
    May 2010

    returning by reference / value.

    What is the difference between returning by reference and returning by value, examples if possible?
    Far greater efficiency is achieved in returning by reference. Memory is saved and
    the program runs faster.

  2. #2
    Registered User
    Join Date
    May 2010
    reference is work like constant pointer ,so all advantages of return by pointer works.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    I suppose returning by value would be this:
    int func(); // prototype
    int x = func();
    whereas returning by reference would be:
    void func(int &x); //prototype
    int n;
    This saves memory and some processing (moving of values) because rather than computing a value in func() and then copying that value into x, as in the first example, the second example computes the value straight into n, so no copying is required. This is more significant if the function requires an input of the same type, eg, if it calculated the square or something. In that case we'd have to alter example one:
    int func(int x);
    More copying.

    Still, "far greater" depends on the context. What's important is that you understand the concept and know how to apply it. It does not have to be your #1 priority or anything (altho C/C++ tends to work this way naturally due to the syntax -- references and pointers are very common and almost impossible to not use).
    Last edited by MK27; 05-28-2010 at 11:01 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2010
    Same concept behind passing by reference and value, really.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    I think MK27 raises a good point: what exactly do you mean by "returning by reference"? I would expect:
    int& foo();
    i.e., the return type of the function is a reference type. MK27's example, on the other hand, illustrates the use of a reference parameter as an in/out or out parameter. For what I expect, the choice between returning by value and returning by reference should not only be made on efficiency grounds. One must consider correctness, as it would be incorrect to return a reference to a local variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM