Not understanding how default parameter values work | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Not understanding how default parameter values work

static int Pow(int x, int y=2) { int result = 1; for (int i = 0; i < y; i++) { result *= x; } return result; } static void Main(string[] args) { Console.WriteLine(Pow(6)); //Outputs 36 Console.WriteLine(Pow(3, 4)); //Outputs 81 } is the Pow method defaulting to multiplying itself by the argument value here? Cannot get my head around this! Any help is appreciated.

13th Aug 2020, 9:25 PM
XCVI P4UL
XCVI P4UL - avatar
1 Answer
+ 3
If you didn't pass the second parameter it will have default value (here it is 2) If you passed, then your second parameter will be as you defined it
13th Aug 2020, 9:27 PM
Marina Vasilyova
Marina Vasilyova - avatar