[Tutor] recursive function example
Alan Gauld
alan.gauld at btinternet.com
Thu Dec 12 10:19:49 CET 2013
Oops, I got this slightly wrong.
On 12/12/13 08:50, Alan Gauld wrote:
> mult(3,0)
>
> It returns zero because b is zero, right?
>
> Now consider what it does for
>
> mult(3,1)
>
> It checks if b is zero, it's not, so it executes
>
> rest = 3 + mult(3,0)
Sorry, it actually does:
rest = mult(3,0)
So rest equals zero.
Because, as we saw above, mult(3,0) returns zero.
Now it executes
value = 3 + 0 # a + rest
so value is now 3
and that's what we return.
The value of mult(3,1) is 3.
> Do you understand this far?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list