Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren0
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)); }
2 Antworten
+ 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;
}
0
Better asked, why does the y power the x