Adding through recursion

Mark Jackson mjackson at alumni.caltech.edu
Fri Nov 18 08:43:53 EST 2005


"martin.clausen at gmail.com" <martin.clausen at gmail.com> writes:
> There is problaly a really simple answer to this, but why does this
> function print the correct result but return "None":
> 
> def add(x, y):
>     if x == 0:
>         print y
>         return y
>     else:
>         x -= 1
>         y += 1
>         add(x, y)
> 
> print add(2, 4)
> 
> result:
> 6
> None

Perhaps this hint will help:

>>> print add(0,6)
6
6

-- 
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
	Of course a weed-puller isn't of much *use* in the Garden
	of Eden, but it takes a while to figure that out.
				- Tim Peters





More information about the Python-list mailing list