1

For an exercise, I have to complete a code that demonstrates the recursion in python.

I have been given and code and told to complete it so for example, that 4^2 = 16

def raise_to_power(base_val, exponent_val):
   if exponent_val == 0:
      result_val = 1
   else:
      result_val = base_val * #start entering code here

   return result_val

user_base = 4
user_exponent = 2

print('%d^%d = %d' % (user_base, user_exponent,
      raise_to_power(user_base, user_exponent)))

I know what they want me to do, but I don't know how to express it. I tried doing base_val ** exponent_val, but it says invalid syntax. It also says not to use math.pow() function.

Can someone help me understand this? I am finding recursion very difficult to get.

4
  • 2
    you are supposed to call the raise_to_power recursively. Commented Mar 8, 2015 at 8:58
  • 3
    You are supposed to solve the problem recusively, so your function raise_to_power needs to call itself at some point again. Maybe this (cs.utah.edu/~germain/PPS/Topics/recursion.html) helps to understand the concept. Commented Mar 8, 2015 at 9:01
  • Yes but how would I call it recursively @AnttiHaapala Commented Mar 8, 2015 at 9:01
  • Thanks @SmCaterpillar I WILL read through that! Commented Mar 8, 2015 at 9:02

3 Answers 3

1

Just simple modification to your code. It's look like your code.

def raise_to_power(base_val, exponent_val):
   if exponent_val == 0:
      return 1
   else:
      return base_val * raise_to_power(base_val, exponent_val-1)

user_base = 4
user_exponent = 2

print('%d^%d = %d' % (user_base, user_exponent,
      raise_to_power(user_base, user_exponent)))
Sign up to request clarification or add additional context in comments.

Comments

1

change function to:

def raise_to_power(base_val, exponent_val):
    return base_val * raise_to_power(base_val, exponent_val - 1) if exponent_val else 1

in recursive functions you must call the function again from within the function body and set one or more conditions to break recursive calls of the function.

For better understanding you can use dis.dis to see the analysis of CPython bytecode by disassembling it :

>>> dis.dis(raise_to_power)
  2           0 LOAD_FAST                1 (exponent_val)
              3 POP_JUMP_IF_FALSE       27
              6 LOAD_FAST                0 (base_val)
              9 LOAD_GLOBAL              0 (raise_to_power)
             12 LOAD_FAST                0 (base_val)
             15 LOAD_FAST                1 (exponent_val)
             18 LOAD_CONST               1 (1)
             21 BINARY_SUBTRACT     
             22 CALL_FUNCTION            2
             25 BINARY_MULTIPLY     
             26 RETURN_VALUE        
        >>   27 LOAD_CONST               1 (1)
             30 RETURN_VALUE        

And for extra info read more about recursion on WIKI.

5 Comments

I don't know if a straight solution without any explanation why it is a case should helpful when it comes to education and homework.
Thanks for your reply. I do not understand what your answer does though. So it calls the function and why did u make exponent_val-1?
What is breaking the call of the function? Why would you do that? Thanks for the edit.
@Elsa Best way to understand these function types mechanism is to trace these by a pen and papers :)
Recursivity is stupid simple, but needs training. You need to do recursion for values N= 0 and/or 1 of you're going to iterate over. Then you do the induction, i.e. how do I get to N+1 given N? And finally you need to define your stop condition, i.e. what is the condition to tell your recursion to stop?
1

Recursive Function:

Recursion is a way of programming a problem, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call. Link


Simple Example to add number from 5 to 0 by recursive call:

Code:

def addNumbers(n):
    print "Debug 1: n=%d"%(n)
    if n==0:
        print "Debug 2: n=%d"%(n)
        return 0
    else:
        print "Debug 3: n=%d"%(n)
        ans = n + addNumbers(n-1)
    print "Debug 4: n=%d, ans=%d"%(n, ans)
    return ans

add_nos = addNumbers(5)
print "\nFinal Addition:", add_nos

Output:

$ python 4.py 
Debug 1: n=5
Debug 3: n=5
Debug 1: n=4
Debug 3: n=4
Debug 1: n=3
Debug 3: n=3
Debug 1: n=2
Debug 3: n=2
Debug 1: n=1
Debug 3: n=1
Debug 1: n=0
Debug 2: n=0
Debug 4: n=1, ans=1
Debug 4: n=2, ans=3
Debug 4: n=3, ans=6
Debug 4: n=4, ans=10
Debug 4: n=5, ans=15

Final Addition: 15

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.