C++ Chapter 15: Recursion
Rules for recursive cases
1) Every recursive definition must have one (or more) base cases. 2) the general case must eventually reduce to the base case. 3) the base case stops the recursion.
two ways of repeating statements in C++
1) looping structures (for, while, do...while) 2) recursive functions
First step in developing a recursive function
Define the problem and find the limiting (base case) solution
recursive algorithm
an algorithm that finds the solution to a problem by reducing the problem to smaller versions of itself; It must have one or more base cases, and the general case must eventually be reduced to a base case
directly recursive
refers to functions that explicitly call themselves
In order to decide whether to call recursively again, a recursive function uses a...
selections structure ("if" or "switch" statement)
recursive definition
statement of a problem solution in which the problem is reduced to smaller versions of itself
Rightmost bit
the binary representation of the remainder after division by 2
base case
the case for which the solution is obtained directly
general case
the case that is recursively defined and eventually reduces to the base case.
Recursion
the process of solving a problem with a self-referencing process or the process of solving a problem by reducing it to smaller versions of itself
rightmost bit (of x )
the remainder of x after division by 2
Why are base cases necessary in recursive solutions?
A recursive solution will call itself, simplifying the problem each time until it reaches a base case. If there is no base case, then infinite recursion will occur
Why can you think of a recursive function as having an unlimited number of copies of itself?
Because every call to the recursive function has its own code and its own set of parameters and local variables
Which is faster, iteration or recursion?
Iteration. Recursion requires a constant allocation and deallocation of memory that makes it slower
Can main be called recursively?
NO
Recursion
The process of solving a problem by reducing it to smaller versions of itself
infinite recursion
When no base case is offered, and the function continues to call upon itself infinitely.
When does recursion terminate?
When the base case is recognized.
recursive definition
a definition in which something is defined in terms of a smaller version of itself
tail recursive function
a function in which the last statement executed is the recursive call.
recursive function
a function that calls itself in its body.
recursive function
a function that calls itself, either directly or indirectly
tail recursive function
a recursive function in which the last statement executed is the recursive call
indirectly recursive
a recursive function that calls another function that results in the original function call
directly recursive
a recursive function that calls itself
recursive algorithm
a solution that solves the problem by breaking it into smaller versions of itself, then using the necessary code to solve.
general case
case In a recursive algorithm, the case where the solution is obtained recursively by simplification on each call
base case
in a recursive algorithm, the case for which the solution is obtained directly
iterative control structures
use a looping structure, such as while, for, or do...while, to repeat a set of statements
indirectly recursive
when a function calls another function that calls upon the first function.
Can a recursive function have more than one base case?
yes