0

Hey guys can anyone explain that output to me?

Don't get why its counting down again.

def kaboom(i):
    print("first Print: ", i)
    if i < 3:
        kaboom(i+1)
    print("second Print: ", i)

OUTPUT:

first Print:  1
first Print:  2
first Print:  3
second Print:  3
second Print:  2
second Print:  1
3
  • Can you show us the whole code where kaboom is used? Commented Mar 11, 2020 at 18:42
  • It's just this small function. I'm learning for an exam Commented Mar 11, 2020 at 18:44
  • there is no context Commented Mar 11, 2020 at 18:44

5 Answers 5

4

Second Print counts down because for every call you make to kaboom(i+1), that call is being put on the top of a stack of calls. Once i >= 3, the stack stops growing and the calls are being popped off where they left off. So 3 is printed and that execution of kaboom(2+1) finishes. Then the call to kaboom(1+1) resumes where it left off and prints 2 and finishes. Finally kaboom(1) resumes and prints 1.

Sign up to request clarification or add additional context in comments.

Comments

3

your function is diving to the farthest call then is coming back and is printing second Print

call 1 --> i = 1             back to call 1 --> second Print, i it is still 1
    ↓                        ↑
    call 2 --> i = 2         back to call 2 --> second Print, i it is still 2 
    ↓                        ↑
    cal 3 --> i = 3 (end of call chain, it goes back)

2 Comments

but how come it is coming back to 1 without havong an explicit -1?
the value of i it doesn't change when it comes back, there is an i variable for every call function,
2

To understand this you need to understand how recursion works. It works on the concept of stacks. You can have a look at the following link, to get a clear understanding : https://www.freecodecamp.org/news/how-recursion-works-explained-with-flowcharts-and-a-video-de61f40cb7f9/

You are calling the same function again if i<3, so it's printing first print and so on.. but after it's returned, the execution won't end there right. It has to execute rest of the method, which is second print.

Comments

1

recursion strategy uses stacks which works as Last In First Out. In your case, If I am right, your base condition is satisfied only at 3 and until then it is stored in the stack and then getting printed.

Comments

1

Think of it this way: you create a new occurrence of kaboom every time it is called. the first time kaboom is called i is one. First is printed and kaboom(2) is called. First is printed and kaboom(3) is called. First is printed and kaboom is not called again because i=3. BUT kaboom(3), kaboom(2), and kaboom(1) are not done yet because they still have to print the second time. Because kaboom(3) was opened most recently it has to close first, hence why second 3 is printed first. Then kaboom(2) prints second 2 and finally kaboom(1) finally finishes by finally printing second 1.

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.