0

Say I have a function

void func (int a = 0, int b = 1)
{
     //do something
}

and I want to give b a value, but not a

func( , 10);

I cannot change the func function, because I have not created it, is there a keyword to say: use default value for this parameter?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 6
    Just specify `b` in the call to the function: `func(b:10);` – maccettura Apr 02 '18 at 13:54
  • 3
    You can't have a semicolon inside the parameter list. –  Apr 02 '18 at 13:55
  • 1
    @Amy: I think it's reasonable to assume that's just a typo, and that the actual method declaration is something like `void Func(int a = 0, int b = 1)`. While it would be nice to see actual code instead of pseudo-code, I think the question can still be answered before the OP confirms that. – Jon Skeet Apr 02 '18 at 13:58

1 Answers1

2

There's no way of explicitly saying "use the default value for this argument", but instead you can avoid specifying any arguments where you don't want the default value. When you want to omit an argument but specify a later one, you have to use named arguments - basically just specifying the name of the parameter that the argument is meant to correspond to. In your case, you want to provide an argument for the b parameter, so you call it like this:

Func(b: 10);

If you only wanted to supply an argument for the a parameter, you could either use a named argument for clarity:

Func(a: 10);

or just use a positional argument:

Func(10); // The compiler assumes this corresponds to the first parameter
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194