[Tutor] Re: recursion question

Lee Harr missive at hotmail.com
Tue Dec 2 17:54:38 EST 2003


>>>def mult(m, n):
        if n == 0:
                return 0
        if n == 1:
                return m
        return m + mult(m, n-1)

>>>mult(5, 5)
25

>So I guess what I'm asking is, does the
>function call within the definition of the function spawn another
>instance of the function?  And then if necessary, that second function
>spawn another?
>


You got it.

Just unroll your sample call:

mult(5, 5)
5 + mult(5, 4)
5 + 5 + mult(5, 3)
5 + 5 + 5 + mult(5, 2)
5 + 5 + 5 + 5 + mult(5, 1)
5 + 5 + 5 + 5 + 5


(Now, you tell me why mult(5, -2) does not work right  :o)

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online 
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963




More information about the Tutor mailing list