[Tutor] Re: recursion sort of

Shantanu Mahajan python@dhumketu.cjb.net
Sun Jun 1 00:48:01 2003


+-- Tom Plunket [python-tutor] [30-05-03 15:47 -0700]:
| 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:
					^ <--- should be 0
| ... 		return number * factorial(number - 1)
| ... 	elif number == 1:
					   ^ <--- should be 0
| ... 		return 1
| ... 	else:
| ... 		return 0
| ... 	
| >>> factorial(4)
| 24
| >>> factorial(6)
| 720
| 

	Regards,
	Shantanu

-- 
Madness has no purpose.  Or reason.  
But it may have a goal.