Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a blank return statement at the end of a function whos return type is void necessary?

Tags:

c++

c

gcc

g++

gnu

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.

like image 985
SwDevMan81 Avatar asked Nov 30 '22 03:11

SwDevMan81


1 Answers

No; that is not needed.

You only need to write return; if you want to return early and skip the rest of the function.

like image 192
SLaks Avatar answered May 19 '23 03:05

SLaks