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)
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.
June 10th, 2013, 07:03 AM
Thank you for the answer.
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".
June 10th, 2013, 09:22 AM
Yes, that sounds accurate.