Adding through recursion

Simon Brunning simon.brunning at gmail.com
Fri Nov 18 08:35:10 EST 2005


On 18 Nov 2005 05:22:47 -0800, martin.clausen at gmail.com
<martin.clausen at gmail.com> wrote:
> 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

Every function returns a value. If you don't use an explicit return
keyword, None is returned implicitly. You print the answer that you
are looking for from within your function, then print the return value
from that function, which, as I've explained, will be None.

--
Cheers,
Simon B,
simon at brunningonline.net,
http://www.brunningonline.net/simon/blog/



More information about the Python-list mailing list