A recursive function repeatedly calls itself, so there needs to be some way of stopping this. Suppose the function is called with a value of 3 - then the first bit is ignored and the line:
myfact = num * myfact(num - 1)
is executed. However, this calls the function again with a reduced number, so that particular execution is suspended while it tries to evaluate
myfact(2)
This again will reach the line:
myfact = num * myfact(num - 1)
and needs to get a result from
myfact(1)
This indeed returns 1, so the previous line can be executed (i.e. 2*1) and then the fist call can also be executed (i.e. 3*2*1).
Hope this helps.
Pete
Bookmarks