10 calling factorial4 factorial4 anint 4 if anint

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: When the factorial func5on calls itself, we are running a new copy of the func5on • The variable an_int has a different value for each recursive call • This does not affect the values of the an_int variable in previous calls! • A func5on cannot return un5l any func5ons it calls have returned… so our original recursive func5on call must wait to return un5l ALL recursive calls have returned! 10 Calling factorial(4) factorial(4) an_int 4 if (an_int <= 1): return 1 else: return an_int * factorial(an_int – 1) 11 Calling factorial(3) factorial(4) an_int 4 factorial(3) an_int 3 if (an_int <= 1): return 1 else: return an_int * factorial(an_int – 1) 12 4 13 ­11 ­04 Calling factorial(2) factorial(4) an_int 4 factorial(3) an_int 3 factorial(2) an_int 2 if (an_int <= 1): return 1 else: return an_int * factorial(an_int – 1) 13 Calling factorial(1) factorial(4) an_int 4 factorial(3) an_int 3 factorial(2) an_int factorial(1) 2 an_int 1 if (an_int <= 1): return 1 else: return an_int * factorial(an_int – 1) 14 Exi,ng factorial(1) factorial(4) an_int 4 factorial(3) an_int 3 factorial(2) an_int if (an_int <= 1): return 1 else: return an_int * (an_int – 1) factorial(1) 2 an_int 1 1 15 5 13 ­11 ­04 Exi,...
View Full Document

This document was uploaded on 03/02/2014.

Ask a homework question - tutors are online