I modified your code to show the exact execution path so you can see why the output is happening.
Modified version of the first code:
def fun(x, depth=0):
print(" " * depth + f"in fun({x}) (depth {depth})")
print(" " * depth + f"if({x} > 0): (depth {depth})")
if(x > 0):
print(" " * depth + f"x = {x} - 1 = {x - 1} (depth {depth})")
x -= 1
print(" " * depth + f"calling fun({x}) (depth {depth})")
fun(x, depth + 1)
print(" " * depth + f"\"{x} \" printed (depth {depth})************")
print(" " * depth + f"x = {x} - 1 = {x - 1} (depth {depth})")
x -= 1
print(" " * depth + f"calling fun({x}) (depth {depth})")
fun(x, depth + 1)
# Driver code
a = 4
fun(a)
Output:
in fun(4) (depth 0)
if(4 > 0): (depth 0)
x = 4 - 1 = 3 (depth 0)
calling fun(3) (depth 0)
in fun(3) (depth 1)
if(3 > 0): (depth 1)
x = 3 - 1 = 2 (depth 1)
calling fun(2) (depth 1)
in fun(2) (depth 2)
if(2 > 0): (depth 2)
x = 2 - 1 = 1 (depth 2)
calling fun(1) (depth 2)
in fun(1) (depth 3)
if(1 > 0): (depth 3)
x = 1 - 1 = 0 (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"0 " printed (depth 3)************
x = 0 - 1 = -1 (depth 3)
calling fun(-1) (depth 3)
in fun(-1) (depth 4)
if(-1 > 0): (depth 4)
"1 " printed (depth 2)************
x = 1 - 1 = 0 (depth 2)
calling fun(0) (depth 2)
in fun(0) (depth 3)
if(0 > 0): (depth 3)
"2 " printed (depth 1)************
x = 2 - 1 = 1 (depth 1)
calling fun(1) (depth 1)
in fun(1) (depth 2)
if(1 > 0): (depth 2)
x = 1 - 1 = 0 (depth 2)
calling fun(0) (depth 2)
in fun(0) (depth 3)
if(0 > 0): (depth 3)
"0 " printed (depth 2)************
x = 0 - 1 = -1 (depth 2)
calling fun(-1) (depth 2)
in fun(-1) (depth 3)
if(-1 > 0): (depth 3)
"3 " printed (depth 0)************
x = 3 - 1 = 2 (depth 0)
calling fun(2) (depth 0)
in fun(2) (depth 1)
if(2 > 0): (depth 1)
x = 2 - 1 = 1 (depth 1)
calling fun(1) (depth 1)
in fun(1) (depth 2)
if(1 > 0): (depth 2)
x = 1 - 1 = 0 (depth 2)
calling fun(0) (depth 2)
in fun(0) (depth 3)
if(0 > 0): (depth 3)
"0 " printed (depth 2)************
x = 0 - 1 = -1 (depth 2)
calling fun(-1) (depth 2)
in fun(-1) (depth 3)
if(-1 > 0): (depth 3)
"1 " printed (depth 1)************
x = 1 - 1 = 0 (depth 1)
calling fun(0) (depth 1)
in fun(0) (depth 2)
if(0 > 0): (depth 2)
Modified version of the second code:
def fun(x, depth=0):
print(" " * depth + f"in fun({x}) (depth {depth})")
print(" " * depth + f"if({x} > 0): (depth {depth})")
if(x > 0):
print(" " * depth + f"calling fun({x-1}) (depth {depth})")
fun(x-1, depth + 1)
print(" " * depth + f"\"{x} \" printed (depth {depth})************")
print(" " * depth + f"x = {x} - 1 = {x - 1} (depth {depth})")
x -= 1
print(" " * depth + f"calling fun({x}) (depth {depth})")
fun(x, depth + 1)
# Driver code
a = 4
fun(a)
Output:
in fun(4) (depth 0)
if(4 > 0): (depth 0)
calling fun(3) (depth 0)
in fun(3) (depth 1)
if(3 > 0): (depth 1)
calling fun(2) (depth 1)
in fun(2) (depth 2)
if(2 > 0): (depth 2)
calling fun(1) (depth 2)
in fun(1) (depth 3)
if(1 > 0): (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"1 " printed (depth 3)************
x = 1 - 1 = 0 (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"2 " printed (depth 2)************
x = 2 - 1 = 1 (depth 2)
calling fun(1) (depth 2)
in fun(1) (depth 3)
if(1 > 0): (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"1 " printed (depth 3)************
x = 1 - 1 = 0 (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"3 " printed (depth 1)************
x = 3 - 1 = 2 (depth 1)
calling fun(2) (depth 1)
in fun(2) (depth 2)
if(2 > 0): (depth 2)
calling fun(1) (depth 2)
in fun(1) (depth 3)
if(1 > 0): (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"1 " printed (depth 3)************
x = 1 - 1 = 0 (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"2 " printed (depth 2)************
x = 2 - 1 = 1 (depth 2)
calling fun(1) (depth 2)
in fun(1) (depth 3)
if(1 > 0): (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"1 " printed (depth 3)************
x = 1 - 1 = 0 (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"4 " printed (depth 0)************
x = 4 - 1 = 3 (depth 0)
calling fun(3) (depth 0)
in fun(3) (depth 1)
if(3 > 0): (depth 1)
calling fun(2) (depth 1)
in fun(2) (depth 2)
if(2 > 0): (depth 2)
calling fun(1) (depth 2)
in fun(1) (depth 3)
if(1 > 0): (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"1 " printed (depth 3)************
x = 1 - 1 = 0 (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"2 " printed (depth 2)************
x = 2 - 1 = 1 (depth 2)
calling fun(1) (depth 2)
in fun(1) (depth 3)
if(1 > 0): (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"1 " printed (depth 3)************
x = 1 - 1 = 0 (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"3 " printed (depth 1)************
x = 3 - 1 = 2 (depth 1)
calling fun(2) (depth 1)
in fun(2) (depth 2)
if(2 > 0): (depth 2)
calling fun(1) (depth 2)
in fun(1) (depth 3)
if(1 > 0): (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"1 " printed (depth 3)************
x = 1 - 1 = 0 (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"2 " printed (depth 2)************
x = 2 - 1 = 1 (depth 2)
calling fun(1) (depth 2)
in fun(1) (depth 3)
if(1 > 0): (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)
"1 " printed (depth 3)************
x = 1 - 1 = 0 (depth 3)
calling fun(0) (depth 3)
in fun(0) (depth 4)
if(0 > 0): (depth 4)