C warning "return" with no value, in a function that returns a non-void

I have this warning.

warning: 'return' with no value, in a function that returns a non-void.

+4
source share
3 answers

You have something like:

int function(void) { return; } 

Add a return value or change the return type to void.

The error message is very clear:

warning: 'return' with no value, in a function that returns a non-void.

Returning without a value is similar to what I showed. The message also tells you that if the function returns "void", this will not give a warning. But since the function should return a value, but your "return" statement did not do this, you have a problem.

This often indicates an ancient code. In the days leading up to the C89 standard, compilers did not necessarily support void. The accepted style was then:

 function_not_returning_an_explicit_value(i) char *i; { ... if (...something...) return; } 

Technically, the function returns int , but no value was expected. This code style triggers the warning you received - and C99 officially outlaws it, but compilers continue to accept it for backward compatibility reasons.

+12
source

This warning also occurs if you forget to add the return statement as the last statement:

 int func(){} 

If you do not specify the return type of the function, by default it should not be invalid, so these are also errors:

 func(){} func(){ return; } 

If you really do not need to return a value, you should declare your function as returning void:

 void func(){} void func(){ return; } 
+4
source

This warning appears when you do this:

 int t() { return; } 

Because t() declared to return int , but the return statement does not return int . The correct version is:

 int t() { return 0; } 

Obviously your code is more complex, but it's pretty easy to define a bare return in your code.

+3
source

Source: https://habr.com/ru/post/1303760/


All Articles