[Tutor] recursion question
Matt Richardson
marichar at csusb.edu
Tue Dec 2 17:20:16 EST 2003
It's been awhile since I've posted last, but I promise I've been a
faithful lurker :) Actually, it's been awhile since I've had a chance
to work with python, so it feels kinda good to ask a question.
I've been taking a c++ class this term and found it to be not as
daunting as I had once feared. The little bit of python I know went a
long way in making the class easier. However, yesterday we touched on
(and I mean briefly touched on) recursive functions. I've seen
questions posted about it before, but the way they work is just out of
grasp for me. Here's the python version of the c++ function I had to
write in the lab last night:
>>> def mult(m, n):
if n == 0:
return 0
if n == 1:
return m
return m + mult(m, n-1)
>>> mult(5, 5)
25
Ok, maybe it's just me thinking about it too much, or not enough, but
I'm just not quite getting how these work. Here's what I do know: exit
cases are needed to keep infinite loops from occuring, the function is
called within it's definition. 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?
in confusion,
Matt
--
Matt Richardson
Instructional Support Technician
Department of Art
California State University San Bernardino
marichar at csusb.edu
(909)880-7727
More information about the Tutor
mailing list