Return void type in C and C++

Viewed 13923

This compiles without any warnings.

Is this legal in C and C++ or does it just work in gcc and clang?

If it is legal, is it some new thing after C99?

void f(){

}

void f2(){
    return f();
}

Update

as "Rad Lexus" suggested I tried this:

$ gcc -Wall -Wpedantic -c x.c 
x.c: In function ‘f2’:
x.c:7:9: warning: ISO C forbids ‘return’ with expression, in function returning void [-Wpedantic]
  return f();

$ clang -Wall -Wpedantic -c x.c 
x.c:7:2: warning: void function 'f2' should not return void expression [-Wpedantic]
        return f();
        ^      ~~~~~
1 warning generated.

$ gcc -Wall -Wpedantic -c x.cc
(no errors)

$ clang -Wall -Wpedantic -c x.cc
(no errors)

Update

Someone asked how this construction is helping. Well is more or less syntactic sugar. Here is one good example:

void error_report(const char *s){
    printf("Error %s\n", s);
    exit(0);
}

void process(){
   if (step1() == 0)
      return error_report("Step 1");

   switch(step2()){
   case 0: return error_report("Step 2 - No Memory");
   case 1: return error_report("Step 2 - Internal Error");
   }

   printf("Processing Done!\n");
}
5 Answers
Related