understanding recursion. introduction zrecursion is a powerful programming technique that provides...

57
Understanding Recursion

Upload: rory-straker

Post on 15-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Understanding Recursion

Page 2: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Introduction

Recursion is a powerful programming technique that provides elegant solutions to certain problems.

Page 3: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Introduction

Recursion is a powerful programming technique that provides elegant solutions to certain problems.

Recursion is a programming technique in which a method calls itself either directly, or indirectly through another method.

Page 4: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

A Mathematical Example -Factorials

Mathematical formulas often are expressed recursively.

Page 5: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

A Mathematical Example -Factorials

Mathematical formulas often are expressed recursively.

In the following example, we will look in depth at factorials.

Page 6: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Definition of Factorial

Factorials - !

The symbol for factorial is “!” - the exclamation mark. The factorial of a positive integer is the product of all nonnegative integers less than or equal to that number.

Zero factorial is a special case and 0! = 1

From this definition, 5! is 120.

5! = 5 . 4 . 3 . 2 . 1 = 120

This formula often is defined recursively, for all nonnegative integers as:

n! = n(n-1)! for n > 0; 0! = 1;

Any number factorial is that number times the factorial of one less

than that number.

Page 7: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

A Closer Look

Now, let’s look at the expression, n! = n * (n-1)! for n > 0; 0! = 1

You will notice that n! subtracts 1 from n, then recomputes the factorial of n-1. This is the recursion.

Page 8: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

A Closer Look

Now, let’s look at the expression, n! = n * (n-1)! for n > 0; 0! = 1

Also notice that the simplest case is 0! This is called the base case.

Page 9: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Base Cases

Base cases are important. A recursive method can solve only a base case.

Page 10: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Base Cases

Base cases are important. A recursive method can solve only a base case.

If the method is called with a base case, it returns a result. If the methods is called with something other than the base case, the recursive method will decide what part it can accomplish, and then call itself to solve the rest of the problem.

Page 11: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

To understand how to program recursively, we will convert the mathematical definition of factorial into code.

n! = n · (n-1)! for n > 0; 0! = 1

Page 12: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

To understand how to program recursively, we will convert the mathematical definition of factorial into code.We’ll start by creating a class, FactorialExample.

public class FactorialExample {

}

n! = n · (n-1)! for n > 0; 0! = 1

Page 13: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

For simplicity, we will add a main method.

public class FactorialExample {

}

n! = n · (n-1)! for n > 0; 0! = 1

Page 14: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

For simplicity, we will add a main method.

The main method will create a FactorialExample object.

public class FactorialExample {

public static void main (String args[]) { FactorialExample fact =

new FactorialExample();

}

}

n! = n · (n-1)! for n > 0; 0! = 1

Page 15: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

We’ll add our recursive method, factorial.

public class FactorialExample {

public long factorial(long number) {

}

public static void main (String args[]) { FactorialExample fact =

new FactorialExample();

}

}

n! = n · (n-1)! for n > 0; 0! = 1

Page 16: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

We now need to identify the base case; that is, the case the method factorial can solve without calling itself.

public class FactorialExample {

public long factorial(long number) {

}

public static void main (String args[]) { FactorialExample fact =

new FactorialExample();

}

}

n! = n · (n-1)! for n > 0; 0! = 1

Page 17: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

In the formula above, we can use 0! = 1 as the base case. 0! is the simplest case.

public class FactorialExample {

public long factorial(long number) {

}

public static void main (String args[]) { FactorialExample fact =

new FactorialExample();

}

}

n! = n · (n-1)! for n > 0; 0! = 1

Page 18: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

In the formula above, we can use 0! = 1 as the base case. 0! is the simplest case.

public class FactorialExample {

public long factorial(long number) {

if (number == 0)

return 1;

}

public static void main (String args[]) { FactorialExample fact =

new FactorialExample();

}

}

n! = n · (n-1)! for n > 0; 0! = 1

Page 19: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

In the formula above, we can use 0! = 1 as the base case. 0! is the simplest case.

However, 1! also equals 1. We can take advantage of this and change the code.

public class FactorialExample {

public long factorial(long number) {

if (number == 0)

return 1;

}

public static void main (String args[]) { FactorialExample fact =

new FactorialExample();

}

}

n! = n · (n-1)! for n > 0; 0! = 1

Page 20: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

In the formula above, we can use 0! = 1 as the base case. 0! is the simplest case.

However, 1! also = 1. We can take advantage of this and change the code.

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

}

public static void main (String args[]) { FactorialExample fact =

new FactorialExample();

}

}

n! = n · (n-1)! for n > 0; 0! = 1

Page 21: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

Now, we need to add recursion.

We will look at the first part of the formula,

n · (n-1)!

If number is greater than 1, we need to compute

n · (n-1)!

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

}

public static void main (String args[]) { FactorialExample fact =

new FactorialExample();

}

}

n! = n · (n-1)! for n > 0; 0! = 1

Page 22: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Converting to Code

Now, we need to add recursion.

We will look at the first part of the formula,

n · (n-1)!

If number is greater than 1, we need to compute

n · (n-1)!

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact =

new FactorialExample();

}

}

n! = n · (n-1)! for n > 0; 0! = 1

Page 23: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Examining the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact =

new FactorialExample();

}

}

The best way to understand recursion is to step through the code.

Page 24: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Examining the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact =

new FactorialExample();

}

}

The best way to understand recursion is to step through the code.

We will use 5! as our test case.

Page 25: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Examining the Code

The best way to understand recursion is to step through the code.

We will use 5! as our test case,

and modify main slightly.

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + “! = “ +

answer);

}

Page 26: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

The code starts by creating a FactorialExample object, fact.

Page 27: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

The testNumber variable is created and set to 5. The answer variable is created.

answertestNumber 5 -

Page 28: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

The factorial method is called.

answertestNumber 5 -

Page 29: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

The formal parameter number is created.

answertestNumber 5 -

number 5

Page 30: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

The formal parameter number is not less than or equal to 1.

answertestNumber 5 -

number 5

Page 31: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

This line is the recursive call.

answertestNumber 5 -

number 5number 5

Page 32: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

This line is the recursive call.

The method will return the value of number (in this case, 5), multiplied by . . .

answertestNumber 5 -

number 5 return: 5 *number 5

Page 33: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

This line is the recursive call.

The method will return the value of number (in this case, 5), multiplied by . . .

The result of the method’s recursive call to itself.

answertestNumber 5 -

number 5 return: 5 *number 5

Page 34: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

The factorial method is called, and another formal parameter number is created.

This time the value of number is the previous formal parameter’s value (number - 1) or 4.

answertestNumber 5 -

number 5 return: 5 *number 5

number 4

Page 35: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

The formal parameter number is not less than or equal to 1.

answertestNumber 5 -

number 5 return: 5 *number 5

number 4

Page 36: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

So, the method will return the value of number (in this case, 4), multiplied by . . .

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

Page 37: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

So, the method will return the value of number (in this case, 4), multiplied by . . .

The result of the method’s recursive call to itself.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

Page 38: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Another formal parameter number is created.

This time the value of number is the previous formal parameter’s value (number - 1) or 3.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

number 3

Page 39: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

The method returns 3 * the result of another recursive call.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

return: 3 *number 3

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Page 40: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

The method returns 3 * the result of another recursive call, with a new formal parameter.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

return: 3 *number 3

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

number 2

Page 41: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

The method returns 2 * the result of another recursive call,

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

return: 3 *number 3

return: 2 *number 2

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Page 42: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

with a new formal parameter.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

return: 3 *number 3

return: 2 *number 2

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

number 1

Page 43: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

The method finally can solve its base case.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

return: 3 *number 3

return: 2 *number 2

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

number 1

Page 44: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

number is equal to 1.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

return: 3 *number 3

return: 2 *number 2

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

number 1

Page 45: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Stepping through the Code

The method returns 1.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

return: 3 *number 3

return: 2 *number 2

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

number 1 return: 1

Page 46: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Stepping through the Code

Control is returned to the calling method.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

return: 3 *number 3

return: 2 *number 2 1

Page 47: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Stepping through the Code

The calling method now can return a value, in this case

( 2 * 1 ) or 2.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

return: 3 *number 3

return: 2 *number 2 1

Page 48: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Stepping through the Code

Control is returned to the calling method.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

return: 3 *number 3 2

Page 49: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Stepping through the Code

The calling method now can return a value, in this case ( 3 * 2 ) or 6.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4

return: 3 *number 3 2

Page 50: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Stepping through the Code

Control is returned to the calling method.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4 6

Page 51: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Stepping through the Code

The calling method now can return a value, in this case ( 4 * 6 ) or 24.

answertestNumber 5 -

number 5 return: 5 *number 5

return: 4 *number 4 6

Page 52: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Stepping through the Code

Control is returned to the calling method.

answertestNumber 5 -

number 5 return: 5 *number 5 24

Page 53: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Stepping through the Code

The last factorial method call will return control to the main method. The method will return the value of (5 * 24) or 120

answertestNumber 5 -

number 5 return: 5 *number 5 24

Page 54: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Stepping through the Code

answer is assigned the value returned by the factorial method call, 120.

answertestNumber 5 120

Page 55: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

public class FactorialExample {

public long factorial(long number) {

if (number <= 1)

return 1;

else

return number * factorial(number - 1);

}

public static void main (String args[]) { FactorialExample fact = new

FactorialExample();long testNumber = 5;long answer;

answer = fact.factorial(testNumber);

System.out.println(testNumber + "! = " +

answer);

}

Stepping through the Code

The following is output to the screen:

answertestNumber 5 120

5! = 120

Page 56: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Summary

Recursion is a powerful programming technique that provides elegant solutions to certain problems.

Recursion is a technique in which a method calls itself either directly, or indirectly through another method.

Base cases are usually the simplest cases a recursive method can solve.

Page 57: Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems

Summary

If the method is called with a base case, it returns a result. If the methods is called with something other than the base case, the recursive method will decide what part it can accomplish, and then call itself to solve the rest of the problem.

The best way to understand recursion is to step through the code.