0

Why does this return None?

def pow(num,num1):
    if num1 < 1:
        print("in if num ",num, "num", num1)
        return num
    else:
        num = num * num
        print("in else num ",num, "num", num1)
        num1 = num1 - 1
        pow(num,num1)

f= pow(2,4)
print(f)
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953

4 Answers4

1

Besides the missing return statement, note that your logic is also wrong. You are repeatedly squaring the number in each step, not mulitplying it with the original number. E.g., for pow(n, 3) your code would calculate ((n²)²)² instead of n*(n*(n)). Also, in order to be a proper pow function (assuming that is what you wanted to write), the num < 1 case should return 1, not num, so that pow(n, 3) returns n*(n*(n*1)) and not n*(n*(n*n)).

def pow(num, num1):
    if num1 < 1:
        return 1
    else:
        return num * pow(num, num1 - 1)

This way, you pass the original value of num to the recursive call, and not the squared value.

(Having said that, for any practical application you should of course use the builtin pow function or the ** operator, just in case you did not know these.)


But why does the value not get returned from the if block? The value is getting updated n each of the recursive calls, if you execute and see the output.

You calculate the value and store it in the local variable num, but that does not change the value of what was previously assigned to that name (numbers are immutable!). Thus you need return to get it out of the function. See this example:

def test(num):
    num = num * 2
    print("inside", num)
    return num

n = 5
r = test(n)
print("outside", n)
print("returned", r)
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • Adding the return works fine. Thank! But why does the value not get returned from the if block? The value is getting updated n each of the recursive calls, if you execute and see the output. – Yaswanth Viswanath Oct 24 '19 at 10:32
  • @YaswanthViswanath **because you aren't returning anything** so by default, it returns `None` – juanpa.arrivillaga Oct 24 '19 at 10:36
0

Because the final pow(num,num1) calls itself without getting the return value from the call and, more importantly, doesn't return the return value that it's not getting :-)

You'll probably find it works better if you return something other then the default None:

return pow(num,num1)

Once you do that, you'll find the return value (final line below) is what you desire:

in else num  4 num 4
in else num  16 num 3
in else num  256 num 2
in else num  65536 num 1
in if num  65536 num 0
65536
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Because you are not returning the value in the recursive call:

pow(num,num1)

try:

def pow(num,num1):
if num1 < 1:
    print("in if num ",num, "num", num1)
    return num
else:
    num = num * num
    print("in else num ",num, "num", num1)
    num1 = num1 - 1
    return pow(num,num1)
roloram
  • 29
  • 2
-1

Your code is working fine as per your input.

output:

    in else num  4 num 4
    in else num  16 num 3
    in else num  256 num 2
    in else num  65536 num 1
    in if num  65536 num 0