Why by changing the position of print statement, in Fibonacci pattern, in recursion, the order of output changes?

Question:

# method 1

def fib(a,b,c,i,x):
    if i==x:
        c = a+b 
        print(c, "first print statement")           # first print statement                             [21]
        return c                    # c = 21, returns to the function itself
    else:
        c=a+b
        a=b
        b =c
        fib(a,b,c,i+1,x)
        print(c,a,i)           # recurssive prive statements                                        [13,8,5,3,2,1]
        return i

print(fib(0,1,1,0,6), "final print statement")               # final print statement                     [0]

In the 1st method, in the else block, below the recursion function print function is given. And hence we obtain a Fibonacci series but in reverse order(from big to small).

def fib(a,b,c,i,x):
    if i==x:
        c = a+b 
        print(a, "first print statement")           # first print statement                         [21]
        return c                    # c = 21, returns to the function itself
    else:
        print(a,i)
        c=a+b
        a=b
        b =c
        fib(a,b,c,i+1,x)
                   # recurssive prive statements                  [13,8,5,3,2,1]
        return i

print(fib(0,1,1,0,6),"last print statement")               # final print statement                        [0]

In the 2nd method, we get the same Fibonacci pattern, but this time it is in the original order (from small to big).

the only difference is the print statement is at 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.

Asked By: ray_milan

||

Answers:

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
Answered By: Basile