225k views
3 votes
Implement a recursive method named power that takes 2 integer parameters named base and expo. The method will return the base raised to the power of expo.

1 Answer

3 votes

Answer:

def power(base, expo):

if expo == 0:

return 1

else:

return base * power(base, expo-1)

Step-by-step explanation:

*The code is in Python.

Create a method called power that takes base and expo as parameters

Check if the expo is equal to 0. If it is return 1 (This is our base case for the method, where it stops. This way our method will call itself "expo" times). If expo is not 0, return base * power(base, expo-1). (Call the method itself, decrease the expo by 1 in each call and multiply the base)

User Vectoria
by
5.5k points