COS202 Lecture 5
COS202 Lecture 5
LECTURE 5
JAVA RECURSION
Introduction
Recursion is the technique of making a function call itself. This technique provides a way to
break complicated problems down into simple problems that are easier to solve. Recursion may
be a bit difficult to understand. The best way to figure out how it works is to experiment with it.
Most mathematical functions that we are familiar with are described by a simple formula. For
instance, we can convert temperatures from Fahrenheit to Celsius by applying the formula
C = 5(F − 32)/9
Given this formula, it is trivial to write any programming language function; with declarations
and braces removed, the one-line formula translates to one line of the source code.
Mathematical functions are sometimes defined in a less standard form. As an example, we can
define a function f, valid on nonnegative integers, that satisfies f(0) = 0 and f (x) = 2f (x − 1) +
x2. From this definition we see that f(1) = 1, f(2) = 6, f(3) = 21, and f(4) = 58. A function that is
defined in terms of itself is called recursive.
1 int f(int x)
2 {
3 if(x == 0)
4 return 0;
5 else
6 return 2 * f(x-1) + x * x;
7 }
Lines 3 and 4 handle what is known as the base case, that is, the value for which the function is
directly known without resorting to recursion. Just as declaring f(x) = 2f (x − 1) + x2 is
meaningless, mathematically, without including the fact that f(0) = 0. Line 6 makes the recursive
call.
It turns out that recursive calls are handled no differently from any others. If f is called with the
value of 4, then line 6 requires the computation of 2 ∗ f(3) + 4 ∗ 4. Thus, a call is made to
compute f(3). This requires the computation of 2∗f(2)+3∗3. Therefore, another call is made to
compute f(2). This means that 2 ∗ f(1)+2 ∗ 2 must be evaluated. To do so, f(1) is computed as
2∗f(0)+1∗1. Now, f(0) must be evaluated. Since this is a base case, we know a priori that f(0) =
0. This enables the completion of the calculation for f (1), which is now seen to be 1. Then f (2),
f (3), and finally f(4) can be determined. All the bookkeeping needed to keep track of pending
function calls (those started but waiting for a recursive call to complete), along with their
variables, is done by the computer automatically. An important point, however, is that recursive
calls will keep on being made until a base case is reached.
Recursion Example
Adding two numbers together is easy to do, but adding a range of numbers is more complicated.
In the following example, recursion is used to add a range of numbers together by breaking it
down into the simple task of adding two numbers.
Example 5.1
1 # Recursion to add all of the numbers up to 10
2 public class Main {
3 public static void main(String[] args) {
4 int result = sum(10);
5 System.out.println(result);
6 }
7 public static int sum(int k) {
8 if (k > 0) {
9 return k + sum(k-1);
10 }
11 else {
12 return k;
13 }
14 }
15 }
When the sum() function is called, it adds parameter k to the sum of all numbers smaller than k
and returns the result. When k becomes 0, the function just returns 0. When running, the program
follows these steps:
10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
Since the function does not call itself when k is 0, the program stops there and returns the result.
CLASS WORK
Given a recursive function f, valid on nonnegative integers, that satisfies f0 = 5 and fn = 2fn-1 + 1.
From this definition, find f5.
ASSIGNMENT
Task: Write a Java program that calculates the factorial of a number using recursion