Thread: How does return "mechanically" work?

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    2

    Question How does return "mechanically" work?

    I'm only a few weeks into my first programming course, and I have a terrible C++ teacher. She attempted to explain the relationship between formal and actual parameters by veering off into CSS order of precedence (clockwise selection of the sides of a box).

    Due to her nonsense "explanations", I have yet to understand how returning a variable is mechanically different from... anything else, really. In fact, I think I just have no idea how functions actually... function.

    I suppose what I'm asking is, how does return actually work?

    Here's an example of what I'm talking about.

    Code:
    int function (int a) {
    a = 5;
    return a();
    }
    vs

    Code:
    void function (int& a) {
    a = 5;
    }

    I understand that using return "returns a value to it's caller", but what is the practical application of that? What is the advantage of using return instead of any other method of assigning a variable? Or... whatever it is that return does?

  2. #2
    Registered User
    Join Date
    Nov 2016
    Posts
    2
    ...So this question is nonsense bull........, basically?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    The first snippet is returning the value returned from calling function a().

    The second snippet is returning nothing (void).

    Now your functions are also being passed values, the first snippet is passing an int value by value. When you pass parameters by value the compiler makes copies of the variable and any changes made to that variables are lost when the function returns.

    The second snippet you're passing the variable by reference. When you pass parameters by reference you are working with the actual value, not a copy, and any change made to the parameter is reflected in the calling function as well.

    You may be interested in the following tutorial for more information.

    Jim

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    I think you're asking, why do we have this "return" concept when the function could just stick its result in a variable?

    Well... it could do that. But the variable would have to be global, otherwise the code that called the function wouldn't be able to look in there.

    But stepping back, think of it this way. You ask somebody "Hey, please add these two numbers and give me the result." The way they give you the result is, they write it on a piece of paper and bury it under the pine tree located 22 feet to the north of 5156 Ambrose Street. That's the analogy for placing the result in a variable.

    Or, the person could like, just tell you the answer instead of making you walk down to 5156 Ambrose Street... that's returning.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It doesn't have to be a global! There is return value optimization and that's why it's considered bad practice to do something like this:
    Code:
    auto f(void) -> user_defined_type
    {
      // ...
      return std::move(user_defined_type{});
    }
    Attempting to return an rvalue reference can break RVO in compilers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't get "return()" to work.Problem in "if" part
    By coder1 in forum C Programming
    Replies: 13
    Last Post: 09-10-2013, 01:08 AM
  2. Replies: 4
    Last Post: 10-18-2012, 08:43 AM
  3. nbin=fopen("input.txt","a"); doesn't work?
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 06-23-2011, 02:57 PM
  4. "return function" doesn't work
    By Cris987 in forum C++ Programming
    Replies: 10
    Last Post: 03-04-2004, 11:04 PM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread