0

Why does this code in the course return 36 and 81?

Cant get my head around it. 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)); Console.WriteLine(Pow(3, 4)); }

16th Nov 2021, 3:23 PM
Miron Ohner
Miron Ohner - avatar
2 odpowiedzi
+ 2
Miron Ohner We have a method with default value y = 2 so when you don't pass 2nd parameter then this default value will work. So in case of Pow(6) loop will be: for (int i = 0; i < 2; i++) { result = result * 6; } In 2nd case loop will be: for(int i = 0; i < 4; i++) { result = result * 3; }
16th Nov 2021, 3:32 PM
A͢J
A͢J - avatar
0
Better asked, why does the y power the x
16th Nov 2021, 3:29 PM
Miron Ohner
Miron Ohner - avatar