Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Doubt about recursion

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
D
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.
 
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.
Thanks very much
 
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.
 
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 😁
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom