In C++11 the following function declaration:
int f(void);
means the same as:
int f();
A parameter list consisting of a single unnamed parameter of non-dependent type void is equivalent to an empty parameter list.
I get the (perhaps false) impression this is an old feature, perhaps inherited from C?
Does anyone know the history or rationale behind this way to declare a function with no parameters?
In computer programming, when void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal. When used in a function's parameter list, void indicates that the function takes no parameters.
In C, an empty parameter list means that the number and type of the function arguments are unknown. Example: int f(); // means int f(void) in C++ // int f( unknown ) in C. In C, it makes sense to avoid that undesirable "unknown" meaning. In C++, it's superfluous.
Declaration void f(void); means that f does not take any parameters. Declaration void f(); means that function f may or may not have parameters, and if it does, we don't know what kind of parameters those are, or how many there is of them.
A void function with value parameters are declared by enclosing the list of types for the parameter list in the parentheses. To activate a void function with value parameters, we specify the name of the function and provide the actual arguments enclosed in parentheses.
In C++ they both mean the same thing.
In C f(void)
is different from f()
, becuse f()
means "unspecified parameters" - you can legally pass anything (whether the function at receiving the data is happy about that or not is another matter).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With