On 10/12/13 14:48, [email protected] wrote:
OK, I'll try again, this time just walking through the code from the top.
def mult(a, b):
if b == 0:
return 0
rest = mult(a, b - 1)
value = a + rest
return value
>
print "3 * 2 = ", mult(3, 2)
We print "3 * 2 = " and then call mult(3,2)
b does not equal zero so we move to the line rest = mult(a,b-1)
This calls mult(3,1)
Now in the new invocation of mult()
b does not equal zero so we move to the line rest = mult(a,b-1)
This calls mult(3,0)
Now in the new invocation of mult()
b does equal zero so we return zero
Now back in the mult(3,1) version of mult()
rest now equals zero
value = a + rest => 3+0 => 3
we return 3
Now back in the original invocation of mult()
rest = 3
value = a+rest +> 3+3 => 6
we return 6
we print 6.
End of program
Remember that each time mult() is called it creates
its own mini-world of variables independent of the
previous calls.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor