Hey guys can anyone explain that output to me?
Don’t get why its counting down again.
JavaScript
x
6
1
def kaboom(i):
2
print("first Print: ", i)
3
if i < 3:
4
kaboom(i+1)
5
print("second Print: ", i)
6
OUTPUT:
JavaScript
1
7
1
first Print: 1
2
first Print: 2
3
first Print: 3
4
second Print: 3
5
second Print: 2
6
second Print: 1
7
Advertisement
Answer
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.