Hello,
I have a doubt about how the following recursion works:
’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’
def fact(n):
if n==1:
return 1
else:
return n*fact(n-1)
print(fact(4))
’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’
The output is 24, which is the factorial of 4.
I know thath Python has a built-in function which is math.factorial(). But in this case, how is “fact” interpreted as the factorial calculator? I think I’m not understanding how recursion itself works.
Could somoeone please clarify me this doubt?
Thanks very much.
I have a doubt about how the following recursion works:
’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’
def fact(n):
if n==1:
return 1
else:
return n*fact(n-1)
print(fact(4))
’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’ ’
The output is 24, which is the factorial of 4.
I know thath Python has a built-in function which is math.factorial(). But in this case, how is “fact” interpreted as the factorial calculator? I think I’m not understanding how recursion itself works.
Could somoeone please clarify me this doubt?
Thanks very much.
Last edited: