问题
I am quite confused with the code below.
def a(x):
print(x)
if x > 0:
a(x - 1)
print(x) #I am confused with this print statement
a(5)
The above code outputs:
5
4
3
2
1
0
0
1
2
3
4
5
Up till the 0
I understand how it prints, but then why it prints in ascending order.
Variable x
is changing so what I thought the output would be is the last assigned value of x that is 0
.
My predicted output:
5
4
3
2
1
0
0
0
0
0
0
0
So how does it track the value of x...?
Can someone explain in brief what actually happens in recursive functions and how variables are stored in it.
回答1:
You have to understand that for each function call the x
is local. So it means that the second call to f
has a different x
. To visualize this: you can see it like:
f(x=5)
print(x) # this x = 5
f(x=4)
print(x) # this x = 4
f(x=3)
print(x) # this x = 3
f(x=2)
print(x) # this x = 2
f(x=1)
print(x) # this x = 1
f(x=0)
print(x) # this x = 0
print(x) # this x = 0
print(x) # this x = 1
print(x) # this x = 2
print(x) # this x = 3
print(x) # this x = 4
print(x) # this x = 5
So in your recursive calls, there are six x
es. Each of the x
variables can have a different value and changing them one level deeper has no impact on the level above, etc.
回答2:
For every call, the function creates new variable x
and prints the respective value of x
in which it was called.
So for each time when the function is called, a new copy of that function is created and so the value of x in that scope. So you are actually creating six x's with it's respective value.
回答3:
Calling a(5)
will start function a()
with x = 5
. This will do three things:
- First print x (which is of value 5)
- Then call
a(4)
- Then (after everything else) print x again (which is still of value 5).
Step 2 just happens to be a bit elaborate, because it also will do these three steps (print "4" twice and call a(3)
in between). This will go down until a(0)
is called which will only print 0 twice but not call a()
again.
回答4:
Let me try to explain. a(4),a(3),a(2) and a(1) are called recursively as they fall in the if statement so in the stack, a(5) is at the bottom and a(1) is on top ,since they are stacked bottom up
def a(x):
print(x) # executed
if x > 0:
a(x - 1) #executed
print(x)
Result: 5,4,3,2,1
When a(0) is called,it doesn't fall into the if statement hence it prints 0 twice. ONLY a(0) is executed to completion,a(1) to a(5) haven't finished executing yet since they are still in the stack.
def a(x):
print(x) #executed
if x > 0:
a(x - 1)
print(x) #executed
Result: 0 , 0
How do we finish executing these function calls? We have to pop them off the stack. Recall i mentioned a(5) is on the bottom and a(1) is on top. Stacks are popped top down hence when a(0) returns, it calls a(1) and a(1) finishes executing its last statement. After a(1) is popped, a(2) is then called and so on.
def a(x):
print(x)
if x > 0:
a(x - 1)
print(x) #executed
Result: 1,2,3,4,5
The stack is now fully popped.
回答5:
This is related to stack.
for every call a different x is created and so the value of x gets prints in the ascending order.
来源:https://stackoverflow.com/questions/42205351/how-variables-are-stored-and-treated-in-recursion-function-in-python