#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    The return statement in C


    Please explain what the reserved word return does.

    My textbook said the "statement transfers control from a function back to the activator of the function. For the function main, control is transferred back to the operating system."

    1. Does the controller/action of a function depend on the function?
    2. What does "activator" of a function mean? Is activating a function different from executing it?
    3. In the statement return (0);, the value 0 is returned as the result of the function execution. What effect does this result have? Also, the 0 looked arbitrary in the context of the program (it converts distance in kilometers to miles). Why is it 0? Does the 0 have any meaning? (I usually associate 0 with "false", but I think this 0 has nothing to do with truth values)
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    Most programmers I know would say "calling" a function instead of "activating" it; but yes, that means the function is executed. The activator or caller of a function would be the function where the call to the function is executed.

    I find it easiest to think of 'return' as "ending" execution of a particular function. Once you call return, no following lines of code from that function are executed. Instead, the program continues executing from the point where it left off when the function was originally called.

    The result returned from the function can be used in any way the programmer sees fit, or it can be ignored. There is no implicit meaning to the return value other than that informally decided upon by the programmer.

    The value 0 does evaluate as false if you use a boolean operator on it.

    One convention is to return 0 on success and a non-zero value on failure. Many operating systems use this convention for the return statement in the main function. However, this convention is far from universal and it would definitely not be safe to assume that your textbook is using it.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    Thank you for the answer.

    I find it easiest to think of 'return' as "ending" execution of a particular function.
    Would it be then acceptable to think of 'return' as paired with the calling of a function? The activator calls the function, and the function does various operations in the program. The function "return"s, and now the operations are done by the activator. If I were to draw a diagram, there would be an arrow going from the activator to the function labeled "call", and another in the opposite direction labeled "return".
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    Yes, that sounds accurate.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo