Returning from a void function [closed]

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;
}

Solution 1:

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.

Solution 2:

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.

Solution 3:

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.

Solution 4:

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.