why these code appear 36 81 | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

why these code appear 36 81

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { 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)); } } }

15th Apr 2018, 10:35 PM
Hovhannes Harutyunyan
Hovhannes Harutyunyan - avatar
3 Respostas
0
y=2 is a default balie. Only used if no other value is specified. Pow(3,4) , 4 is specified and used. A specified value overrides the default. Pow (6) , no value is specified so 2 is used
16th Apr 2018, 10:31 AM
sneeze
sneeze - avatar
+ 2
Pow(6) means x is set to 6 inside the function, however it also means y defaults to 2 (due to the y=2). This means the loop executes twice, meaning result = 1 * 6, the first time, then 6*6 the second time. The function then returns result, which equals 36. With Pow(3, 4) x = 3, and y = 4 This means the loop executes four times... so result = 1 * 3 result = 3 * 3 result = 9 * 3 result = 27 * 3 function then returns result, which is now 81.
15th Apr 2018, 10:45 PM
Emma
+ 1
okay great but one more question I have you wrote x = 3 y =4 , but in upstairs y is given 2 ...
15th Apr 2018, 10:49 PM
Hovhannes Harutyunyan
Hovhannes Harutyunyan - avatar