-
Recursive Function
I am trying to work with the recursive function. I would like to implement a program using the recursive function power that when invoked, returns 3 to the power of 4. e.g., (3,4) = 3*3*3*3. I would like to assume that the exponent is an integer greater than or equal to 1. I would like to take the user input for the base.
My thoughjt process is that the recursion step would use the relationship:
base[exponent] = base*base [exponent]-1
-
Looks good so far
Now just write the code for it - pretty much as you've written it now really
Dont forget the simple condition which stops the recursive calls.
-
If this is an excersice, it's a bad example of recursion. An iteration had been much better in this problem.
Anyway, something like this should solve it:
Code:
int MyPower(int Base, int Exp)
{
if(Exp <= 0) return 1;
else return Base * MyPower(Base, Exp - 1);
}
-
cat < message_board > tutor
-
by the way
hi all ,
by the way ... how can we impement this funciton .... ?
I mean how could they impement this fuction in C++.
thanx