1

I am reading the book The C Programming Language. At the end of page 26 the paragraph that comes is,

A function need not return a value; a return statement with no expression causes control, but no useful value, to be returned to the caller, as does "falling off the end" of a function by reaching the terminating right brace.

What does "falling off the end" mean in the above written paragraph and also in general?

4

This is arguably a programming question and not an English question, but as it's ambiguous, I'll go ahead and answer. :-)

The idea is this: Suppose you were walking down a pier that sticks out into the water. You get to the end and try to keep walking. You will "fall off the end". That is, you have gotten to the end of the pier, and now you fall into the water.

In a function, normally there is a "return" statement to tell the compiler that it should end processing and return to the caller. If a function has no return statement, you get to the end of the function and, what is the compiler supposed to do? Instead of ending with a proper return statement, processing just "falls off the end". The statement you quote says that in that case the function will return, just as if you had ended it with a return statement.

  • Your are essentially correct. However, for most programming languages, if there is not something to be returned, the standard is not to end the function with a return; – Kevin Jan 15 '15 at 14:38
  • I got what "fall off the end" means. But I don't get how it fits in the context. Where does the compiler fall? Moreover there is a return statement, but with no expression.(...a return statement with no expression...) – user31782 Jan 15 '15 at 14:41
  • No, he says that a return with no expression returns no useful value, "AS DOES falling off the end". That is, giving a return with no expression is the same as "falling off the end". He refers to reaching the end of the function and the last statement NOT being a return as "falling off the end". That's what I meant about walking off the end of a pier. You get to the end of the function, and if there is no return statement, then there is nothing to explicitly tell the compiler what to do. So: it "falls off the end" of the function. It reaches the end and if it tried to keep going it would ... – Jay Jan 15 '15 at 16:40
  • ... fall off into nothingness. So the writer is saying that what happens in this case is that the function returns to the caller without returning a meaningful value -- just like if you'd put a return statement with no expression. – Jay Jan 15 '15 at 16:41
  • Is a return statement with no expression equivalent to having no return statement at all? – user31782 Jan 15 '15 at 18:03
1

"Falling off" means to "fall down from", so "falling off the end" means to "fall down from the edge of" (something unknown, think of a table).

Of course, here it's used metaphorically:

Once all steps of the function have been executed, the control is handed back to the calling function. (-> The return statement has the same effect, but at some other point in the code - e.g. after a branch or if-statement.)

  • Do you mean that the return statement deceives the compiler to believe that there aren't more steps to execute? Why does the compiler fall if there is a return statement with no expression. And where does it fall? – user31782 Jan 15 '15 at 15:09
  • Fall off the end simple means "reach the end" and continue from there. For the programmer: assume there are the functions foo() and bar(). foo() does something, then calls bar(). Within bar() either all steps are executed, or, at some point, a return-statement is reached. Both cause the control to go back to the caller foo(). Simple example: bar() is supposed to calculate a mathematical formula. When it checks the parameters it might discover that the operation may not be calculated (wrong parameters) and returns before reaching the end of the calculation. Else, it just executes all steps. – Stephie Jan 15 '15 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.