tags:

views:

598

answers:

9

I see functions/methods with a void return in the signature that have a return statement at the end of the function. What is the reason for this, and does this apply to other languages?

For all I know, I can use return if I want to exit anywhere else but the end of the function.

A C example:

void funtion(void)
{
   int x = 1 + 2;

   return;    // what do we need this for, if at all?
}
+3  A: 

As you mention yourself, you only need the return statement if you want to exit the function before the last statement. In the given example the return statement serves no purpose whatsoever.

Stephan202
+1  A: 

My guess it is because function used to return some value and a programmer probably thought that it will again.

Mykola Golubyev
+2  A: 

It is indeed pointless... at least to the compiler, but stylistic, much like many things in C++. An example:

int main ()
{
   DoSomething ();
   return 0;
}

In C++ the return 0 is entirely pointless as far as a conforming compiler is concerned. Yet sometimes people prefer to include redundant or meaningless statements just to be explicit. I'd be hesitant to chalk these kinds of things up to programmer ignorance. It's just stylistic, feel free to get rid of it if you feel like it.

Dan Olson
Yep. It does no harm, the compiler optimises it away, and some programmers legitimately believe it increases readability.
slim
Not pointless to a command line which looks at the return code.
Jimmy J
It's still pointless, in standard C++ main need not end with a return statement despite being declared to return int. If there's no return, 0 is assumed. This is all standard, so return 0 is pointless there, yet most people prefer to put it there explicitly.
Dan Olson
@Jimmy J - I believe 0 would be returned implicitly. You only need the explicit return if it's nonzero.
slim
Dan, most people learned that from the textbooks, I think. I didn't know that the standard says "it's ok for int main to have no return statement", good info, though. thx
hasen j
Whether you like it or not, main() has a return value. There's no way to enforce this in code so C++ treats main() differently, automatically fixing up bad code (because there was so much of it in existence at the time).
Jimmy J
It's not pointless. It demonstrates that the programmer has thought about the return code (and presumably the program can't fail). It is a form of documentation.
dangph
+1 to dangph. It's rarely pointless to be explicit.
Brian Neal
It's consistent. All the other `int` functions have explicit `return`s at the end.
aib
I agree with you guys, it's rarely pointless to be explicit. Updating my answer to reflect that it's pointless *to the compiler*.
Dan Olson
+19  A: 

This seems pointless here. But my guess is that this kind of thing can be used to put a breakpoint in IDEs which don't support putting a breakpoint at the closing brace and by putting a breakpoint here, some values can be checked in the watch window etc.

Aamir
You got a good point there! Though, I think when doing such thing, one must clean up the make-debugger-happy leftover code when he's done debugging.
hasen j
It will not help for breakpoints (you can try it). There is no real code generated by return from void function. Not every keyword transfers to a code.
Suma
@Suma: You seem to have a good chance of being correct. That's why I said it to be a guess in my original post.
Aamir
I dont have any other IDE than Visual Studio to try this right now. VS automaticaly shifts such a breakpoint to the closing brace. Closing brace roughly translates to popping out the return address from stack and moving the instruction pointer to that address.
Aamir
so, it is quite possible that other IDEs support this type of thing through return statement. As I said I dont have any other IDE right now to try this so can't be sure.
Aamir
This is very insightful
MeThinks
gcc doesn't produce any different code for the return version either.
Pete Kirkham
@hasen j - Correct! This may be an excuse for putting it in the code, but it's not an excuse for leaving it in the code.
Daniel Earwicker
+1  A: 

It's useless in C/C++ and just reduces readability.

One real language example where this is needed is assembler. But in assembler the return statement is not a part of language. Instead you have to invoke a special instruction (ret in x86) which causes return of control. Unless you do it execution just continues and what will happen next depends on may factors, luck and moonphase included.

sharptooth
I agree that it may stem from other languages needing it to handle pushing and popping values from the stack properly, etc.
Joe Philllips
+11  A: 

Someone took a 'a function must have a single return' coding guideline too literally.

Pete Kirkham
That is great!!! :-)
Subtwo
It wouldn't surprise me if this was true.
Daniel Earwicker
+3  A: 

I always include a return statement at the end of my functions, even when they don't return anything. My original reasons for it were that I was being explicit, there was no ambiguity about where the function ended, I always got irked by functions that "walked off the end". Afterwards, it was simply a habit, hard to break (if I wanted to).

sykora
+3  A: 

For a function that returns void, it doesn't matter, since "falling off the end" of a void function simply returns as you would expect.

However some people like to do this to aid future maintenance. For example, if someone later decides to change the return type of the function to say int, you should get a compiler error if you forget to change that final return statement to make it return an int value. If you failed to have a return statement at all, the return value is undefined, and many older compilers wouldn't warn you about this (it's true - I've seen it!). This isn't much of a issue these days, as most modern compilers will warn about these things.

Brian Neal
+1  A: 

Some compilers can optimize code based on return statements.
"Use the Return statement whenever your logic permits it. For more information, see Return Statement. The compiler can optimize the code better than if you use Exit Function, Exit Property, or Exit Sub, or allow the End Function, End Get, End Set, or End Sub statement to generate a return"

SwDevMan81
thanks SwDevMan81. This appears to refer to Return Statement in relation to the other VB ways of exiting a Sub/Function.
MeThinks