[Tutor] Re: recursion sort of

Tom Plunket py-tutor@fancy.org
Fri May 30 18:48:01 2003


Jennifer Cianciolo wrote:

> cycle +=3D1  # and what is this? / is this why it's recursive?

A recursive function is one that calls itself, so this function
is not recursive.  Recursion is a cool solution to a number of
problems that are not conveniently solved with looping, but a
simple one is the factorial function, where factorial(N) means
the product of each number between 1 and N, so if N is 4, the
result is 1 * 2 * 3 * 4.  (Many of you probably know that
already, but it's for anyone who doesn't.  <g>)

>>> def factorial(number):
... 	if number > 1:
... 		return number * factorial(number - 1)
... 	elif number =3D=3D 1:
... 		return 1
... 	else:
... 		return 0
... 	=09
>>> factorial(4)
24
>>> factorial(6)
720


-tom!