I can't understand how it works. Why the output has the first line, where var X == 1 ?
And, how compiler doesn't give an error if new_var is not defined anywhere as a integer ?
Is it because of call stack works like that ? It returns from the last call, so after returning 1, new_val becomes == 1 ?!
Output:
I'm here!!!! X is: 1
I'm here!!!! X is: 2, and new_var is 1
I'm here!!!! X is: 3, and new_var is 2
I'm here!!!! X is: 4, and new_var is 6
24
def fact_rec(x):
if x > 0:
if x == 1:
print(f"I'm here!!!! X is: {x}")
return 1
else:
new_var = fact_rec(x-1)
print(f"I'm here!!!! X is: {x}, and new_var is {new_var}")
return new_var*x
else:
return "It's negative"
print(fact_rec(4))
