Omit void as function result in C++

Viewed 285

In C/C++ there are two ways to declare a function that returns nothing. First one is to declare a function without arguments:

// no arguments
void f() {
}

The second one is to declare a function that takes void:

// still no arguments
void f(void) {
}

However, this is not true for the function result: we can't omit void at the beginning of the function like this:

// returns nothing
f() {
  return; // yay
}

So, is there absolutely no way to omit void at the beginning of the function like with the function arguments?

1 Answers

So, is there absolutely no way to omit void at the beginning of the function like with the function arguments?

No, there absolutely isn't. There is no function declaration syntax that doesn't have the return type in C++ nor in C99 or later. There used in C prior to C99, but the default return type would have been int rather than void.

Note that declarations void f() and void f(void) are not equivalent in C. Former declares a function with unspecified parameters while the latter declares a function with empty parameter list. In C++, both declare a function with empty parameter list.

Related