In the 1st method, in the else block, there is a call to the print function below the recursive call. And hence we obtain a Fibonacci series but in reverse order (from big to small).
# method 1
def fib(a,b,c,i,x):
if i==x:
c = a+b
# first print statement [21]
print(c, "first print statement")
return c # c = 21, returns to the function itself
else:
c = a+b
a = b
b = c
fib(a,b,c,i+1,x)
# recursive print statements [13,8,5,3,2,1]
print(c,a,i)
return i # not the Fibonacci number
# final print statement [0]
print(fib(0,1,1,0,6), "final print statement")
In the 2nd method, we get the same Fibonacci pattern, but this time it is in the original order (from small to big).
# method 2
def fib(a,b,c,i,x):
if i==x:
c = a+b
# first print statement [21]
print(a, "first print statement")
return c # c = 21, returns to the function itself
else:
# recursive print statements [1,2,3,5,8,13]
print(a,i)
c = a+b
a = b
b = c
fib(a,b,c,i+1,x)
return i # not the Fibonacci number
print(fib(0,1,1,0,6),"last print statement") # final print statement [0]
The only difference is the print statement moved to the very beginning of the else block.
How is this possible?
How, by changing the position of the print statement, the entire order is changed?
We know that in recursion, while the stack comes down, the values come out of the memory, and the output is printed (from bigger value to the least value present at the depth of the stack).
So the order of the printed output should remain the same for both the conditions, isn't it?
Please help me to know, how changing the position of the print statement in the else block changed the order of output.
You are on the right track with your reasoning, however consider that with each recursive call that you do in your 1st method (print after call to fib), you will first go down the recursions until you hit the exit condition. this will print 'first print statement' and then, it will execute the print after the call to fib with the last computed value. And then we go up the recursion layers, hereby printing the fibonaci sequence in reverse.
A way of seeing is by immagining boxes inside one another. With the print after the call to fib you will print from the last to the first value and with the print before you will print the first to the last.
You can try this little program that demonstrates the levels of recursion :
def recursive_function(level):
if level >= 5:
print('[ Level', level, ']-You reached the bottom of the call stack. Time to go back up in reverse')
else:
print('[ Level', level, ']-I get printed first and then you move down to level', level+1, 'of the call stack')
recursive_function(level+1)
print('[ Level', level, ']-I won\'t get printed unless you return from the level', level+1)
if __name__ == '__main__':
recursive_function(0)
which prints :
[ Level 0 ]-I get printed first and then you move down to level 1 of the call stack
[ Level 1 ]-I get printed first and then you move down to level 2 of the call stack
[ Level 2 ]-I get printed first and then you move down to level 3 of the call stack
[ Level 3 ]-I get printed first and then you move down to level 4 of the call stack
[ Level 4 ]-I get printed first and then you move down to level 5 of the call stack
[ Level 5 ]-You reached the bottom of the call stack. Time to go back up in reverse
[ Level 4 ]-I won't get printed unless you return from the level 5
[ Level 3 ]-I won't get printed unless you return from the level 4
[ Level 2 ]-I won't get printed unless you return from the level 3
[ Level 1 ]-I won't get printed unless you return from the level 2
[ Level 0 ]-I won't get printed unless you return from the level 1