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; }
3 Réponses
+ 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.
+ 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.
+ 1
@Tamra
Maybe 'i' is used as a counter because it's commonly an integer, and the first letter is used as representation.