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

Slides:



Advertisements
Similar presentations
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Advertisements

CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Programming with Recursion
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Recursion CITS1001.
Warm Up #4 1. Evaluate –3x – 5y for x = –3 and y = 4. –11 ANSWER
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Recursion James Wright St Peter’s Secondary School 733 Parkhill Road West Peterborough,Ontario K9J-8M4
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
CS 108 Computing Fundamentals March 31, Grades not as good as I hoped I drop the lowest of the first 3 exams Exam #3 and #4 will cover much of the.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Introduction to Computer Programming
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion Reading L&C 3 rd : 7.1 – nd :
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
C++ Programming Lecture 12 Functions – Part IV
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
Understanding Recursion
Recursion.
Jeopardy Solving Equations
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Chapter 15 Recursion.
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Java Software Structures: John Lewis & Joseph Chase
Chapter 12 Recursion (methods calling themselves)
CS1120: Recursion.
Recursion.
CS302 - Data Structures using C++
Java Programming: Chapter 9: Recursion Second Edition
CS148 Introduction to Programming II
Learning Objective Students will be able to: Solve equations in one variable that contain absolute-value expressions.
Recursion.
Presentation transcript:

Understanding Recursion

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

Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems. zRecursion is a programming technique in which a method calls itself either directly, or indirectly through another method.

A Mathematical Example - Factorials zMathematical formulas often are expressed recursively.

A Mathematical Example - Factorials zMathematical formulas often are expressed recursively. zIn the following example, we will look in depth at factorials.

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 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.

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.

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.

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

Base Cases zBase cases are important. A recursive method can solve only a base case. zIf 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.

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

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

Converting to Code For simplicity, we will add a main method. public class FactorialExample { } n! = n · (n-1)! for n > 0; 0! = 1

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

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

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

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

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

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

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

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

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

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.

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.

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); }

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.

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-

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-

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

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

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 5 5

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

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

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 4

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 4

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

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

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 3

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); }

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

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); }

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

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

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

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

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

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

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

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

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

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

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

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

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 5120

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 ! = 120

Summary zRecursion is a powerful programming technique that provides elegant solutions to certain problems. zRecursion is a technique in which a method calls itself either directly, or indirectly through another method. zBase cases are usually the simplest cases a recursive method can solve.

Summary zIf 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. zThe best way to understand recursion is to step through the code.