0

Given the example function:

def FooBar(foo_exp):
    if Bar:
            update_Foo(foo_exp)                      
            FooBar(foo_exp)
    else:
        newNode = fooNode(foo_exp)
        print "The value of newNode is ",newNode

        return newNode

The following results when called with a = FooBar(foo_exp):

The Value of NewNode is foo_exp - When called inside the class FooBar

But the variable a is populated with a value of type None (As if the function returned nothing)

Would the recursion in this function cause this? Is there some other factor at play?

A = Type None even when return NewNode is commented out. This leads me to believe Python returns type None on functions without an explicit return Value.

5
  • Where does the Bar variable come from? Is it a global? Commented Mar 23, 2015 at 6:16
  • Also where did rootnode come from? Commented Mar 23, 2015 at 6:55
  • @AndrewMagee Bar is local to the function- assume it always fires at least once. I think this is more a problem with my abstraction of my example code - but I see your point that Bar is never updated in this example. Commented Mar 25, 2015 at 22:25
  • @zehnpaard I've updated the code a bit for clairity, you're correct that rootnode was an error. Commented Mar 25, 2015 at 22:25
  • 1
    Please post a Minimal, Complete, and Verifiable example. You haven't given us enough code to diagnose the problem. Show us the code that calls FooBar as well--preferably a self-contained program that we can simply copy and paste and run. Commented Mar 25, 2015 at 22:35

3 Answers 3

1

Your recursive function has two branches: If Bar (whatever that is) is not truthful, the function returns some new node. Otherwise, the function is recursively called—but there is no return value.

So if you want to always have some return value, you should make sure that all code paths return some value. For example, you could return the value from the recursive function call:

return FooBar(foo_exp)
Sign up to request clarification or add additional context in comments.

Comments

0

Are you missing a return in the if block?

if Bar:
    update_Foo(foo_exp)                      
    return FooBar(foo_exp)

Comments

0

The else clause looks like your base case. You need to return the result of the recursion..

    return FooBar(foo_exp)

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.