68

Which is more correct way to return from function:

void function() {
  // blah some code
}

OR

void function() {
  // blah some code
  return;
}

Rationale for second way:

  1. It expresses developer intentions more clearly.
  2. It helps detecting function end at pre-compile time:

Suppose you have such scenario- you have bunch of functions and you must inject some code at the end of those functions. But for some reasons you don't want / or can't modify such huge amount of functions. What can you do about that ? Return & macro comes into play, for example:

#include<stdio.h>

#define MAX_LINES 1000
#define XCAT(a,b) a##b
#define CAT(a,b) XCAT(a,b)
#define return returns[__LINE__] = 1;\
        if (returns[__LINE__])\
           {printf("End of function on %d line.\n",__LINE__);}\
        int CAT(tmp,__LINE__); \
        if ((CAT(tmp,__LINE__)=returns[__LINE__], returns[__LINE__] = 0, CAT(tmp,__LINE__)))\
              return

static int returns[MAX_LINES];


void function1(void) {
    return;
}

void function2(void) {
    return;
}

int main()
{
    function1();
    function2();

    return 0;
}
Agnius Vasiliauskas
  • 10,413
  • 5
  • 46
  • 66
  • 3
    Interesting Question, but voted to close because it is far too subjective, and likely to cause some conflict. Possibly better for programmers.stackoverflow.com. Also remember to read the FAQ. – Richard J. Ross III Jan 25 '12 at 13:12
  • 4
    It's rather a question of style to insert redundant keywords. – stacker Jan 25 '12 at 13:17
  • @stacker - what is redundant for one situation, it is not for another... – Agnius Vasiliauskas Jan 25 '12 at 14:05
  • 2
    Overloading a keyword is really dangerous, in particular if it is done as unqualified as it is done here. If the return statement is the depending statement of an `if` you completely change the semantics of the program. Don't do such things. – Jens Gustedt Jan 25 '12 at 16:09
  • @Jens Gustedt - I've fixed example on cases when return is dependent on if. Seems now example don't breaks program semantics... I agree - doing this is too dangerous. But what do you think about direct post question ? – Agnius Vasiliauskas Jan 25 '12 at 22:27
  • Such people may come from the world of assembly language, where a `ret` instruction is compulsory – linquize Sep 11 '13 at 06:26

4 Answers4

76

Neither is more correct, so take your pick. The empty return; statement is provided to allow a return in a void function from somewhere other than the end. No other reason I believe.

roottraveller
  • 6,328
  • 4
  • 50
  • 59
William Morris
  • 3,294
  • 1
  • 20
  • 23
66

The only reason to have a return in a void function would be to exit early due to some conditional statement:

void foo(int y)
{
    if(y == 0) return;

    // do stuff with y
}

As unwind said: when the code ends, it ends. No need for an explicit return at the end.

Salek
  • 193
  • 4
  • 14
Tevo D
  • 3,221
  • 18
  • 28
27

The first way is "more correct", what intention could there be to express? If the code ends, it ends. That's pretty clear, in my opinion.

I don't understand what could possibly be confusing and need clarification. If there's no looping construct being used, then what could possibly happen other than that the function stops executing?

I would be severly annoyed by such a pointless extra return statement at the end of a void function, since it clearly serves no purpose and just makes me feel the original programmer said "I was confused about this, and now you can be too!" which is not very nice.

unwind
  • 364,555
  • 61
  • 449
  • 578
1

An old question, but I'll answer anyway. The answer to the actual question asked is that the bare return is redundant and should be left out.

Furthermore, the suggested value is false for the following reason:

if (ret<0) return;

Redefining a C reserved word as a macro is a bad idea on the face of it, but this particular suggestion is simply unsupportable, both as an argument and as code.

don provan
  • 51
  • 3