I'm having concerns on how the loop flow works in Python when Recursion is used (never mind the function, just learning about loop flow)
def factorial(n):
print("first line")
if n == 1 or n == 0:
return 1
result = n * factorial(n-1)
print('line after result')
print('current result is ' + str(result) )
print('before return result')
return result
In my example I used factorial(3) and I'm getting it like this:
first line
first line
first line
line after result
current result is 2
before return result
line after result
current result is 6
before return result
6
How come I get line after result three times after result is computed/returned. Am I not supposed to get it only once after the result? How many times does the loop return the result? I don't understand how recursion loop flow works in python. Please help. Thanks