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!
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.
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.
It means that there is no default return value for your function outside of the for loop.
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.
-Wreturn-type
. It's enabled by -Wall
(which you should always be running with, along with -Werror -Wextra
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With