-2

My recrusive function returns None even when I print the variable on the above line

When I call the function it prints exactly what I want, but returns None!

def nRound(vector, root):
    tempRoot = root
    a = vector.pop()
    b = vector.pop()
    if  a+b < 1.0:
        vector.append(a+b)
        rootn = Node(a+b)
        rootn.right = tempRoot
        rootn.left = Node(b)
        nRound(vector, rootn)

    else:    
        rootn = Node(a+b)
        rootn.right = tempRoot
        rootn.left = Node(b) 
        print(rootn)   
        return rootn 

I don't understand why it returns None instead rootn. Thanks in advance.

1 Answers1

0

Your function is recursive, and only the base case returns a value. Values from the recursive call are not passed upward:

    nRound(vector, rootn)

That means an external caller will only get a value if the function immediately reaches the base case. The above line should be

    return nRound(vector, rootn)
Christoph Burschka
  • 4,467
  • 3
  • 16
  • 31
  • Thanks for your time, but it didn't work for me... I use the debugger and when it reaches de ```return rootn``` the compiler goes back to the recursive function when I want it leaves the entire function with the result "rootn". Maybe I'm missing some fundamental in recursive function in Python... – André Luiza Jun 03 '19 at 16:08