return by reference


I am having confusion regarding return by reference and return by value.

why return by reference is used when return by value can be used?what is the advantage of using return by reference when compared to return by value?

please help..
Hi,

Return by value means the whole object is copied, if it is big then that's inefficient. Return by by reference essentially just returns the memory address, which is usually a zillion times better.

Return by reference is also used, when one wants to change the values of a variables, and have that change reflected in the scope where the function is called. This handy because we change the value of several variables, whereas a function can only return 1 value.

Have a look at the tutorial at the top left of this page.

http://www.cplusplus.com/doc/tutorial/functions/


There is a section Arguments passed by value and by reference
When return by reference is used instead of return by value?why return by reference is needed?
TheIdeasMan wrote:
Return by value means the whole object is copied, if it is big then that's inefficient. Return by by reference essentially just returns the memory address, which is usually a zillion times better.



Following the link I gave above, there is this section also:

Efficiency considerations and const references
Hey. Watch his video, Bucky explains it very well - https://www.youtube.com/watch?v=wAmq8eIkdI8
Topic archived. No new replies allowed.