returning by reference / value.

This is a discussion on returning by reference / value. within the C++ Programming forums, part of the General Programming Boards category; What is the difference between returning by reference and returning by value, examples if possible? Far greater efficiency is achieved ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    62

    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
    Posts
    18
    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
    Location
    segmentation fault
    Posts
    8,300
    I suppose returning by value would be this:
    Code:
    int func(); // prototype
    int x = func();
    whereas returning by reference would be:
    Code:
    void func(int &x); //prototype
    int n;
    func(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:
    Code:
    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
    Posts
    109
    Same concept behind passing by reference and value, really.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    I think MK27 raises a good point: what exactly do you mean by "returning by reference"? I would expect:
    Code:
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21