Most of the questions on SO refer to non-void return types, but we are having a flame war at work about this and wanted to find out what the community thought.
void DoSomething()
{
return; // Is this needed?
}
From this discussion, it looks like the issue of having an undefined behavior deals with functions of non-void return types. Do void return types have this same undefined behavior, or is it only in the non-void returning function?
My concern is that this will just end up as a terrible coding style that isn’t justified by anything. However if it’s also an undefined behavior for void return functions, then I can see the need for adding it to the coding standard. If the answer is different for C vs C++ this is ok too.
§ 6.6.3 The return statement
2 A return statement without an expression can be used only in functions that do not return a value, that is, a function with the return type of void, a contrsuctor(12.1), or a destructor(12.4).
§ 6.6.3/2
Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.
No; that is not needed.
You only need to write return;
if you want to return early and skip the rest of the function.
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