I think somethings wrong with this one? How can it work when the 'x' variable is not use in the code ? What is the 'i' variable | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I think somethings wrong with this one? How can it work when the 'x' variable is not use in the code ? What is the 'i' variable

static int Pow(int x, int y=2) { int result = 1; for (int i = 0; i < y; i++) { result *= x; } return result; }

11th Feb 2017, 7:08 AM
Pháp Lương
Pháp Lương - avatar
3 Answers
+ 6
x is being used in the code - the for is accessing it's value to multiply by. i is defined in the for to serve as the counter. It's very common to see i as the counter variable, I don't know why that's a convention.
11th Feb 2017, 7:26 AM
Tamra
Tamra - avatar
+ 1
Is the creating an output? Such as running through the console and not being called? Or is a value being put through for x, such as Pow(1, 2) or Pow()? As for what the 'i' variable is, it's a counter used to create a limit of how many loops will run. Without it, the loop will be infinite and that is bad in programming.
11th Feb 2017, 7:30 AM
James Burton
James Burton - avatar
+ 1
@Tamra Maybe 'i' is used as a counter because it's commonly an integer, and the first letter is used as representation.
11th Feb 2017, 7:32 AM
James Burton
James Burton - avatar