Optional argument | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

12th Jun 2020, 3:08 PM
Sanket Sharma
Sanket Sharma - avatar
2 Answers
+ 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
12th Jun 2020, 4:25 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
Thanks sir
12th Jun 2020, 4:30 PM
Sanket Sharma
Sanket Sharma - avatar