Checking return value of a function without return statement
Plea开发者_JAVA百科se explain why sometimes return statement is not needed?
Function has a return type, but return statement missing. Meanwhile, program compiles and works fine.
Please help me understand this better
char* handleInput() {
fgets(buffer, 1024, stdin);
// return buffer; <---- COMMENTED RETURN
}
void main() {
char* ptr = handleInput();
int flag = atoi(ptr);
if (flag < 0) break;
printf("You entered: %s\n", ptr);
}
Basically what gets returned is dumb luck. You get what happens to be in the CPU register when it comes back. If, for example, the returned value would be in AX, and the char*
happens to be in AX, you lucked out. I believe this is an undefined behavior; i.e. the C language specifications don't tell what you should so, so it is left to the compiler. I'm surprised a modern compiler wouldn't at least throw a warning at you.
C99 6.9.1/12 "Function definitions" says:
If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
C90 6.6.6.4 "The return
statement"standard says something with simialr effect:
If a
return
statement without an expression is executed, and the value of the function call is used by the caller, the behavior is undefined. Reaching the}
that terminates a function is equivalent to areturn
statement without an expression.
So returning from a function without returning something from the function is permitted - but the caller of the function isn't allowed to use the 'result' of the function call. That's undefined behavior, and like other answers have mentioned you can get 'expected' results with undefined behavior, but that's only by chance.
I believe the rationale for this is that pre-standard C defaulted to a return type of int
for functions if the function wasn't explicitly declared (or if the declaration omitted the return type), and that continued to be supported when C was standardized. Many of those functions were designed to be called only for side-effects they produced, and no return value was expected or provided.
A couple side notes:
- gcc will warn about this if the
-Wall
option is used. MSVC warns about it by default. - you should be getting a compiler error about the
if (flag < 0) break;
line since thebreak
isn't in a loop or switch. main()
should returnint
notvoid
(-Wall
will warn about that as well). Of course, you should also return some value explicitly...
Not using 'return' in a function declared to return a value is bad practice to say the least. Most compilers should generate a warning.
精彩评论