+ 1
Optional argument
static int Pow(int x, int y=2) { int result = 1; for (int i = 0; i < y; i++) { result *= x; /*need explanation this line*/ } return result; } static void Main(string[] args) { Console.WriteLine(Pow(6)); Console.WriteLine(Pow(3, 4)); } // anyone explain me how it work
2 Antworten
+ 4
Basically, the line you ask for means:
result = result * x;
In other words, the loop goes from 0 to whatever you enter as second argument (or 2, by default) and will execute result * x this many times.
So, Pow(4,3) will execute:
(1 *) 4 * 4 * 4 ==> 64
Pow(5) will execute:
(1 *) 5 * 5 ==> 25
+ 1
Thanks sir