Daemon
Daemon

Reputation: 1675

What is the use case for default argument without name in the function signature

Taken from cpp reference, https://en.cppreference.com/w/cpp/language/default_arguments

void f(int, int = 7); // #2 OK: adds a default

Is there any particular use case where this is useful, considering there is no name to the default argument.

Or I am just reading too much between the lines and should just leave with the understanding that it is just the function signature declaration and in function definition one has to provide the variable name to make any use of the second parameter in the function.

I was expecting the variable name in the function signature.

Upvotes: 1

Views: 190

Answers (3)

machine_1
machine_1

Reputation: 4454

Parameter names are not mandatory in function declarations nor in function definitions.

void f(int, int = 7);

int main()
{
    f(1);
    return 0;
}

void f(int, int)
{
    return;
}

Compiles without errors...

However, if you want to make use of the parameters then you must give them names in order to be able to reference them.

With function declarations, you do not reference the parameters but giving them names is useful for you and readers of your code; the compiler doesn't care.

In function definitions is where you reference the parameters, so here is where you have to provide them with names:

#include <iostream>

void f(int, int = 7);

int main()
{
    f(1);
    return 0;
}

void f(int a, int b)
{
    std::cout << b << std::endl;
    return;
}

Output: 7

Upvotes: 3

Fareanor
Fareanor

Reputation: 6805

void f(int, int); is a function declaration, which is different of function definition.

In the declaration, we (the compiler) only need to know that we declare a function f that takes 2 ints and returns a void.

The names of the parameters are not necessary here since we do not refer to them.

But in the definition, it's another story. In that case, we need to specify parameters names in order to be able to use them.

For example:

// Declaration
void f(int, int);

// Definition
void f(int a, int b)
{
    std::cout << a << ',' << b << '\n';
}

In the above example, we could not use the a and b in the function definition if we did not give them those names. But in the declaration, we didn't care.

Note: It may still be useful to specify names in the declaration for documentation purposes (or simply to help the human reader).

Note 2: Actually you can also omit the parameter name in the definition, but in that case, you won't be able to refer to it.
It is sometimes used when we plan to define the function body later and do not want to have a "unused parameter" warning while compiling (Although I personally prefer to keep and comment out the parameter name instead).

Upvotes: 3

Jesper Juhl
Jesper Juhl

Reputation: 31458

You can leave out the argument name in the function declaration. The compiler just needs to know that the function takes two ints and that one has a default value. If the argument is needed in the definition then a name needs to be provided. But, there are use cases where you might even leave out the argument in the definition.

Maybe a bit contrived, but one example could be a function that used to only take one argument and can now take two, but you want to keep old code compiling and the second argument is optional and use of the second argument has not yet been implemented. In that case you might start by adding the second argument to the function signature with some reasonable default and people can now start calling the function with two arguments as well as just one as usual and then in the future you expand your implementation to actually make use of (and thus name) the second argument in the function definition.

Upvotes: 0

Related Questions