15B. Recursion
Recall that, whenever a function is called,
it gets a new frame.
So each call has its own variables.
A consequence of that is that a function can call itself.
It actually calls
a copy of itself, with a new frame. A function that calls
itself is said to be recursive,
or to use recursion.
Example
Let's write a definition of function factorial(n) that returns
n! = 1×2×…×n. The following two
facts should be clear.
0! |
= |
1 |
(by definition) |
n! |
= |
n×(n−1)! |
(if n > 0) |
For example,
5! |
= |
5×4×3×2×1 |
|
= |
5×(4×3×2×1) |
|
= |
5×4! |
Those facts suggest the following definition of factorial,
which works correctly as long as n ≥ 0.
int factorial(int n)
{
if(n == 0)
{
return 1;
}
else
{
return n * factorial(n-1);
}
}
Detailed hand simulation of factorial
Let's use frames to simulate computation of
factorial(3). To make the simulation easier to show, we break the
statement in the else-part into statements. Here is the modified
definition of factorial.
int factorial(int n)
{
if(n == 0)
{
return 1;
}
else
{
int r = factorial(n-1);
return n * r;
}
}
Initially, a frame for factorial(3) is created.
Since n is not 0, factorial moves to the else part
and calls factorial(2).
factorial |
n = 3 |
at: int r = factorial(n-1); |
|
|
|
The call to factorial(2) sees that n is 2, not 0, so it
goes to the else part and calls factorial(1).
factorial |
n = 3 |
at: int r = factorial(n-1); |
|
|
factorial |
n = 2 |
at: int r = factorial(n-1); |
|
|
|
Since n is not 0, the call to factorial(1)
calls factorial(0).
factorial |
n = 3 |
at: int r = factorial(n-1); |
|
|
factorial |
n = 2 |
at: int r = factorial(n-1); |
|
|
factorial |
n = 1 |
at: int r = factorial(n-1); |
|
|
|
Since n is 0, the call to factorial(0) returns 1.
factorial |
n = 3 |
at: int r = factorial(n-1); |
|
|
factorial |
n = 2 |
at: int r = factorial(n-1); |
|
|
factorial |
n = 1 |
r = 1 |
at: return n * r; |
|
|
Factorial(1) returns 1*1 = 1.
factorial |
n = 3 |
at: int r = factorial(n-1); |
|
|
factorial |
n = 2 |
r = 1 |
at: return n * r; |
|
|
Factorial(2) returns 2*1 = 2.
factorial |
n = 3 |
r = 2 |
at: return n * r; |
|
|
Finally, factorial(3) returns 3*2 = 6.
Exercises
What is the value of g(4), given the definition of
g below? (Hint. Work out g(1), then g(2),
then g(3), then g(4), in that order. Keep your work
organized.)
int g(int n)
{
if(n == 1)
{
return 2;
}
else
{
return g(n-1) + 3;
}
}
Answer