CS2
Recursion
6
Local variables
nEach recursive call to the method creates a “new copy” of the method – all the local variables are created again
n• The compiler keeps track of them all so that the programmer cannot mix them up
nfactorial(4)has a parameter n equal to 4
nfactorial(3) has its own parameter n, equal to 3
nfactorial(2) has its own parameter n, equal to 2
nfactorial(1) has its own parameter n, equal to 1
n• This means that you should not use recursion to replace
nstraightforward iteration (like factorial), particularly if the loops are performed thousands or millions of times.