Recursion (Module 4) Flashcards
What is a programming technique where a method calls itself to solve a problem, and will continue to call itself until a certain condition (base case) is met?
recursion
How does recursion work?
breaking down a problem into smaller sub-problems, then solving each sub-problem by recursively calling the method
In recursion, what is used to keep track of method invocations?
stack
What is a factorial?
the product of a number, n, and every number below it (6! = 6 * 5 * 4 * 3 * 2 *1)
Let’s say that we call factorial(6). What happens to it? Then what happens if we call factorial(5)?
- factorial(6) is pushed to the call stack. When factorial(5) is called, it’s pushed to the top of the call stack
What must every recursive program include a ________ case.
base (stopping)
What is a condition under which a method stops making recursive calls?
base (stopping case)
What happens when a a stopping case is absent?
infinite recursion
What happens when infinite recursion occurs?
it leads to runtime errors and a stack overflow
What formulas define a sequence in terms of previous terms in the sequence?
recursive formulas
What formulas define a sequence directly, without referencing previous terms?
iterative formulas
What are the advantages of recursion?
- it makes code shorter and cleaner
- required in data structures and advanced algorithms
What are the disadvantages of recursion?
- it takes a lot of stack space (memory)
- takes more processing time
- more difficult to debug