default argument

Oct 11, 2014 at 7:43pm
Can someone tell me what a default argument is in a function?

1
2
3
4
5
6
7
8
9
10
bool isLegal(int age, int minAge = 21)
{
 return age >= minAge;
}

legal = isLegal(age); // same as isLegal(age, 21)
if (inLouisiana())
{
 legal = isLegal(age, 18);
}


The call isLegal(age) is completely equivalent to isLegal(age, 21). C++ just
provides the default argument for you. The call to isLegal(age, 18) ignores the
default value.
Last edited on Oct 11, 2014 at 7:45pm
Oct 11, 2014 at 7:44pm
Oct 11, 2014 at 7:51pm
So, a default argument is not set to a value in the declaration? I can't really tell, microsoft always explains things in a way that's hard to understand.
Oct 11, 2014 at 9:13pm
closed account (SECMoG1T)
This is legal int minAge = 21
Topic archived. No new replies allowed.