Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`f(void)` meaning no parameters in C++11 or C?

Tags:

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?

like image 882
Andrew Tomazos Avatar asked Aug 22 '13 09:08

Andrew Tomazos


People also ask

Is void a parameter in C?

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.

What is void function without 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.

What is F void?

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.

Can a void function take in parameters?

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.


1 Answers

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).

like image 123
Mats Petersson Avatar answered Sep 25 '22 13:09

Mats Petersson