# Recursive Function

Printable View

• 05-09-2002
Lisa Mowbray
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
• 05-09-2002
Salem
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.
• 05-09-2002
Magos
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); }```
• 05-09-2002
Salem
cat < message_board > tutor
• 05-09-2002
NANO
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