Default values in parameters

Why is it not valid syntax to do something like this? It's not important, it's just that it would seem it is valid.

void MyFunction(int a, int b = 2, int c, int d){/*blah*/}

Then call it like this
MyFunction(1, , 3, 4);
?

It gives an error that in the function definition, c and d are missing their default parameters. I was under the understanding that default parameters didn't have to be the last ones! The wording in the tutorial is what confused me:
This value will be used if the corresponding argument is left blank when calling to the function.


This gave me the impressing that I could simply skip passing a parameter and it would use the default value.

Like I said, I am simply curious. It's not important, I can just make my function parameters out of order (or add a default value for all of them :S )
No, the way you're doing it, you are literally sending a value (void). When you define default values, you always define with the last one being your starting point:
1
2
3
4
5
void MyFunction(int a, int b = 2, int c = 3, int d = 4){/*bla*/} // legit way to use default values

MyFunction(3); // MyFunction(3, 2, 3, 4);
MyFunction(3,8); // My Function(3, 8, 3, 4);
// and so on 

It's also possible to define a default for all the parameters, in that case you would write just MyFunction(). Do NEVER forget the parenthesis!
You answered a question I didn't ask, but you answered my original question at the same time anyway. Thanks, I can see how it would cause problems now. :)
Last edited on
Topic archived. No new replies allowed.