Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++ allow unnamed function parameters?

The following is a perfectly legal C++ code

void foo (int) {     cout << "Yo!" << endl; }  int main (int argc, char const *argv[]) {     foo(5);      return 0; } 

I wonder, if there a value to ever leave unnamed parameters in functions, given the fact that they can't be referenced from within the function.

Why is this legal to begin with?

like image 619
James Raitsev Avatar asked Aug 29 '12 21:08

James Raitsev


People also ask

Can a function have no parameters in C?

If a function takes no parameters, the parameters may be left empty. The compiler will not perform any type checking on function calls in this case. A better approach is to include the keyword "void" within the parentheses, to explicitly state that the function takes no parameters.

What happens when you create a function without any parameters in C?

In C, void no_args() declares a function that takes an unspecified (but not variable) number of parameters (and returns nothing). So all your calls are valid (according to the prototype) in C. In C, use void no_args(void) to declare a function that truly takes no parameters (and returns nothing).

Can functions have no parameters?

The lesson brief states that “Functions can have zero, one or more parameters”.

Are function parameters identifiers?

x identifies a parameter of your function, which can be used as a variable (identifier) in calculations. So yes, x is an identifier.


2 Answers

Yes, this is legal. This is useful for implementations of virtuals from the base class in implementations that do not intend on using the corresponding parameter: you must declare the parameter to match the signature of the virtual function in the base class, but you are not planning to use it, so you do not specify the name.

The other common case is when you provide a callback to some library, and you must conform to a signature that the library has established (thanks, Aasmund Eldhuset for bringing this up).

There is also a special case for defining your own post-increment and post-decrement operators: they must have a signature with an int parameter, but that parameter is always unused. This convention is bordering on a hack in the language design, though.

like image 158
Sergey Kalinichenko Avatar answered Oct 14 '22 07:10

Sergey Kalinichenko


Of course not naming a parameter is legal when just declaring the function, but it's also legal in the implementation. This last apparently strange version is useful when the function needs to declare the parameter to have a specific fixed signature, but the parameter is not needed.

This may happen for example for a method in a derived class, for a callback function or for a template parameter.

Not giving the parameter a name makes clear that the parameter is not needed and its value will not be used. Some compilers if you instead name a parameter and then simply don't use it will emit a warning that possibly there is a problem with the function body.

like image 41
6502 Avatar answered Oct 14 '22 05:10

6502