Hey all, I am looking for help in understanding how recursive functions work. What I want is a function to be given two integer paramaters x and n, where the recursive function is essentially supposed to calculate x^n noting that x^(n-1)*x=x^n. The function should return the proper answer.
Here is the code regarding the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int power(int x, int n)
intconst y = x; //I did this so that we never lose what the initial value of x is.
if (n ==1)
x=x*y; n--; return power(x, n);
I'm working on another recursive function. I want to use ONLY recursive calls, no loops. The program should print n asterisks on a single line, then print a new line, and then print one less asterisk, until there are no more. For example, calling printStars(5) would do:
Once it recursively calls the function n times, n is then zero and the function is done. How can I set it up so that n does not go to zero so I can start a new line? I can easily do this problem with a loop, but i want to with only recursive functions. Thanks