0

It is possible to set more than one default arguments with the same type at the end of the parameter list?

int myfunc(int a, int, b, int c = 42, int d = 137);
{
...
}

Now if i call myfunc(x1, x2, x3); than c=x3 and d=137?
or d=x3, and c=42 or this is not valid?

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Fbody
0

That function definition is not valid as written. Remove the semi-colon then it will be valid.

You may set as many default parameters as you like regardless of the data types. You just need to be sure that any parameters with default values are at the end of the parameter list. Any required parameters (parameters that don't have default values) must be at the beginning of the list. Once you begin assigning default values to the parameters, you may no longer have a parameter that doesn't have a default value.

int myfunc(int a, int, b, int c = 42, int d = 137); //valid declaration
int myfunc(int a = 42, int, b, int c, int d = 137); //NOT a valid declaration

Based on the provided function declaration, the call myfunc(x1, x2, x3) would produce:
a = x1
b = x2
c = x3
d = 137

However, the call myfunc(x1, x2, , x3) would produce:
a = x1
b = x2
c = 42
d = x3

In other words, what you get depends on how you call the function.

Edited by Fbody: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.