0

hi I wrote the code below, i was expecting the function to return 8 but somehow this function doesn't return any value at all, is there a way to modify this function so it can return some value? (please still use a recursive function). Also it would be much appreciated if you can let me know why this problem occurred.

Thanks!

 def recursive(left):
    if left<=7:
        left+=1
        recursive(left)
                      
    else:
        return left

recursive(0)

3 Answers3

3

When you call the function you should use the return to get the result back to the called function.

def recursive(left):
    if left<=7:
        left+=1
        return recursive(left)
                      
    else:
        return left
Sivaram Rasathurai
  • 5,533
  • 3
  • 22
  • 45
1

It should be

def recursive(left):
    if left<=7:
        left+=1
        return recursive(left)
                      
    else:
        return left

recursive(0)
user3431635
  • 372
  • 1
  • 7
0

After the recursive(left) the function ends, so it has an implicit return None there.

gkimbar
  • 11
  • 2