#### guiromero

##### Coder
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.

Last edited:
Solution
Recursion, in this case, is just a function calling itself. It may sound strange at first and you have to get used to the idea, but just realize there is really no difference, none at all, between a function calling itself or a function calling another function. Except that the first is called recursion 🙂 This `fact(4)` is evaluated in 4 steps (or 5 if you wish) :

Code:
``fact(4) = 4*fact(3) = 4*3*fact(2) = 4*3*2*fact(1) = 4*3*2*1 = 24``

Hope this helps. Oh and please post your code (especially Python code) in proper code tags, using the `</>` button.
Recursion, in this case, is just a function calling itself. It may sound strange at first and you have to get used to the idea, but just realize there is really no difference, none at all, between a function calling itself or a function calling another function. Except that the first is called recursion 🙂 This `fact(4)` is evaluated in 4 steps (or 5 if you wish) :

Code:
``fact(4) = 4*fact(3) = 4*3*fact(2) = 4*3*2*fact(1) = 4*3*2*1 = 24``

Hope this helps. Oh and please post your code (especially Python code) in proper code tags, using the `</>` button.

• guiromero
Recursion, in this case, is just a function calling itself. It may sound strange at first and you have to get used to the idea, but just realize there is really no difference, none at all, between a function calling itself or a function calling another function. Except that the first is called recursion 🙂 This `fact(4)` is evaluated in 4 steps (or 5 if you wish) :

Code:
``fact(4) = 4*fact(3) = 4*3*fact(2) = 4*3*2*fact(1) = 4*3*2*1 = 24``

Hope this helps. Oh and please post your code (especially Python code) in proper code tags, using the `</>` button.
Thanks very much

Did it help ?

Yeah, it really helped. Thanks.

• • 