1. ## strange strange functions

hi
in this book there is a lesson called functions.
i guess that n is going to be the exponent of m so far.

the following code is directly from the book:

Code:
```#include<stdio.h>

int power ( int m, int n);

main ()
{
int i;
for (i=0; i<10;++i)
printf("%d %d %d\n", i, power(2, i), power(-3, i));
return 0;
}
int power(int base, int n)
{
int i, p;

p=1;
for(i=n;i<=n;++i)
p = p * base;
return p;
}```
i need help with this code because i dont understand it.
an explanation of the whole code line by line would be very appreciated.

2. Line by line explanation?! Come on, there must be some of it you understand already. What do you think it's doing?

There's a bug in it too, I'm afraid, on this line:
>>for(i=n;i<=n;++i)
I'll let you see if you can find it.

3. i found the bug hammer

this for statement is useless.
because 'i' is setted equal to n so he cant enhance the value.

i still dont understand why there are other vars in the prototype than in the function. shouldnt they be the same?

i dont understand why to take excatly these variables like i, p, base and n.
and why has the function power to return p??
doesnt the following line declare that ie 2 has the exponent i and -3 too? so only the output is written down in main and the arithmetic is declared in power.

you see the more i write about functions the more i understand them, keep helping me
Code:
`printf("%d %d %d\n", i, power(2, i), power(-3, i));`
another question would be, why p is declared as 'p * base' in power. i thought parameter names were local to power and not visible to other functions....

sorry to be a bit annoying, but if i am not annoying to you, ill never understand this.

4. The variable names used in the function prototype don't have to be the same as those use on the function itself, although it's bad style to mix them. It's the type of variable that's important, in this case it's 2 ints. This means you can ignore m.

i is used as a loop counter only.
p is used to collate the result of the power calculation. For example:
2*2*2=8
... is done in stages, so the first time through the loop, p is 1 and the calc looks like this:
>p = p * base;
>p = 1 * 2; /* so now p is 2 */
... and the next time...
>p = 2 * 2; /* so now p is 4 */
... and the next time...
>p = 4 * 2; /* so now p is 8 */
... and when we've looped enough, we simply return the value to the caller:
>return p;

Any help?

5. that was a big step forward for me

thanks