Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc options: warning on non-void functions without a return statement

Does anyone know a gcc/g++ option that generates an error/warning if there's a function that has a non-void return value but doesn't contain a return statement in its definition?

e.g.:

int add(int a, int b)
{
    a+b;
}

Many thanks in advance!

like image 816
c0f33.alex Avatar asked Mar 29 '12 11:03

c0f33.alex


People also ask

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.

How do I clear 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.

What does non void function does not return a value in all control paths?

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

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

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.


1 Answers

-Wreturn-type. It's enabled by -Wall (which you should always be running with, along with -Werror -Wextra).

like image 134
Oliver Charlesworth Avatar answered Sep 28 '22 08:09

Oliver Charlesworth