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

#### cbreemer

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

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

#### guiromero

##### Coder
Yeah, it really helped. Thanks.

#### hanginthere

##### Coder
I just want to add a note about recursion. It's a good technique for certain things but is very bad in some cases. Whenever you call a function the system must make a note of where to return when the call is done. Typically it keeps this (and other information) on a stack. In the case of calculating factorial 10, the stack is not that deep, but when you start calculating larger factorials you can quickly run out of stack space. So factorial is a good example in explaining recursion, but a bad example of when to use it.

#### cbreemer

##### King Coder
I just want to add a note about recursion. It's a good technique for certain things but is very bad in some cases. Whenever you call a function the system must make a note of where to return when the call is done. Typically it keeps this (and other information) on a stack. In the case of calculating factorial 10, the stack is not that deep, but when you start calculating larger factorials you can quickly run out of stack space. So factorial is a good example in explaining recursion, but a bad example of when to use it.
Good point. Although I would not want to say recursion is ever a "bad technique", you certainly need to know what you're doing. As with all power tools 😁