It's fairly simple: you define a function
foo
with parameter
x
, inside of
foo
is another function called
bar
, which also has a (separate) parameter
x
. When you call
foo(3)
, it returns
bar(x * 2)
, which in this case is equivalent to
bar(6)
, which in turn returns
x + 1
, or in this case, 6 + 1, which equals 7. The syntax is a little weird, because most common languages do not allow proper functions (as opposed to lambdas and anonymous functions) inside of other functions, and even worse since both have the same parameter name. It might be a little clearer if they had different parameter names, like:
def foo(x):
def bar(y):
return y + 1
return bar(x * 2)
foo(3)