1

Look at the code below:

def foo (x):

    print("foo environment: x = {0}".format(x))
    def bar (z, x = 0):
        print("bar environment: z = {0} and x = {1}.  Value to be returned: {2}".format(z, x, x+z))
        return z + x

    return bar(3)

foo(5)

foo environment: x = 5
bar environment: z = 3 and x = 0.  Value to be returned: 3
3

Since in the foo environment x = 5, why bar uses the value 0?

1 Answer 1

2

You are only passing one argument here:

return bar(3)

The bar function accepts two values, one z and one x (x has a default value (0) and that is why only 1 argument is enough). By only passing the z, x=0.

Try this, and see what happens:

return bar(3, x) 
Sign up to request clarification or add additional context in comments.

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.