-
Recursion
I have a exercise which asks that a function be writen that returns the value of base and exponent using recursive function.
for example; integerpower (3,4) = 3*3*3*3. Assume that the exponent is a positive nonzero integer, and base is an integer. The function should use for to control the calculation. Can you explain what the original call code will make this work?
-
Please restate your question. I find that a lot of people post questions about programming, but should really be learning to write english instead.
-
I think you want something more like this:
Code:
/* Recursive factorial function */
#include <stdio.h>
long integerpower (int base, int exponent);
int main()
{
int x, y = 2;
for ( x = 1; x <= 10; x++ )
printf ("%d^%d\tis %d\n", y, x, integerpower ( y, x ) );
return 0;
}
long integerpower (int base, int exponent)
{
if (exponent == 1)
return base;
else
return(base * integerpower(base, exponent -1));
}
-Prelude