Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does flowing off the end of a non-void function without returning a value not produce a compiler error?

Tags:

c++

c

gcc

g++

Ever since I realized many years ago, that this doesn't produce an error by default (in GCC at least), I've always wondered why?

I understand that you can issue compiler flags to produce a warning, but shouldn't it always be an error? Why does it make sense for a non-void function not returning a value to be valid?

An example as requested in the comments:

#include <stdio.h> int stringSize() { }  int main() {     char cstring[5];     printf( "the last char is: %c\n", cstring[stringSize()-1] );      return 0; } 

...compiles.

like image 422
Catskul Avatar asked Oct 22 '09 21:10

Catskul


People also ask

What does non-void function does not return a value?

It means that there is no default return value for your function outside of the for loop.

What happens if you forget the return statement in a non-void function?

You may or may not use the return statement, as there is no return value. Even without the return statement, control will return to the caller automatically at the end of the function.

What does control reaches end of non-void function mean?

After executing programs, sometimes we get the error: 'warning: control reaches the end of non-void function', which means that certain functions that would have to return some values attain the termination. It might not give any value later.

How do I fix error control reaches end of non-void function?

If control reaches the end of a function and no return is encountered, GCC assumes a return with no return value. However, for this, the function requires a return value. At the end of the function, add a return statement that returns a suitable return value, even if control never reaches there.


1 Answers

C99 and C++ standards don't require functions to return a value. The missing return statement in a value-returning function will be defined (to return 0) only in the main function.

The rationale includes that checking if every code path returns a value is quite difficult, and a return value could be set with embedded assembler or other tricky methods.

From C++11 draft:

§ 6.6.3/2

Flowing off the end of a function [...] results in undefined behavior in a value-returning function.

§ 3.6.1/5

If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0; 

Note that the behaviour described in C++ 6.6.3/2 is not the same in C.


gcc will give you a warning if you call it with -Wreturn-type option.

-Wreturn-type Warn whenever a function is defined with a return-type that defaults to int. Also warn about any return statement with no return-value in a function whose return-type is not void (falling off the end of the function body is considered returning without a value), and about a return statement with an expression in a function whose return-type is void.

This warning is enabled by -Wall.


Just as a curiosity, look what this code does:

#include <iostream>  int foo() {    int a = 5;    int b = a + 1; }  int main() { std::cout << foo() << std::endl; } // may print 6 

This code has formally undefined behaviour, and in practice it's calling convention and architecture dependent. On one particular system, with one particular compiler, the return value is the result of last expression evaluation, stored in the eax register of that system's processor.

like image 146
fnieto - Fernando Nieto Avatar answered Oct 17 '22 06:10

fnieto - Fernando Nieto