Oleksiy
Oleksiy

Reputation: 39889

Is there a reason for an empty return statement at the end of void function?

I was just looking at someone else's code and they have an empty return statement at the end of a void function:

void someFunction (int* someArg, int someArg2, int someArg3) {

    // some operations/function calls/recursion

    return;
}

Is there a particular reason why it should be there?

Upvotes: 4

Views: 2072

Answers (3)

Freddy Pierson
Freddy Pierson

Reputation: 443

Personally, I think it's nice to keep things explicit. Thus, I would prefer to use return in a void function. Whether you use it or not is up to you, but it helps with readability.

Upvotes: 2

Status BreakPoint
Status BreakPoint

Reputation: 114

In ISO/IEC 14882:2003 [Programming languages - C++] below.

6.6.3 The return statement

  1. A function returns to its caller by the return statement.

Upvotes: -2

maditya
maditya

Reputation: 8916

There's no reason it needs to be there at the very end of a function, as far as I know. It's possible the function originally returned a value, someone changed it to a void, and just replaced return value; with return;. Or someone not very experienced with C++ assumed that every function must have a return, and will blindly believe this to the bitter end.

Now, a return in the middle of a function is definitely relevant since it stops the execution of the function at that point.

Upvotes: 8

Related Questions