[Tutor] Recursive function(posting the output of teh code)

Tom Plunket py-tutor@fancy.org
Sat May 31 16:14:01 2003


evros loizides wrote:


> evrything okey until i have to use recursive function taht i am not familiar
> with how does it work .
> Any good reference on recursive functions taht will be helpfull too.

A recursive function is a function that calls itself.  For
instance:

>>> def silly_print_string(s):
... 	if len(s) > 0:
... 		print s[0]
... 		silly_print_string(s[1:])
... 		
>>> silly_print_string("test")
t
e
s
t
>>> silly_print_string("another test")
a
n
o
t
h
e
r
 
t
e
s
t

this recursive function prints the first letter of the string and
then calls itself with the remainder of the string.  This
function definition may also give you a hint.

-tom!