Recursive Functions
Chapter overview: Recursive functions provide a mathematical way to define computations through smaller instances of the same problem. This chapter connects recursion with the idea of general-purpose computation.
4.1 Recursive Functions
Introduction / Definition
A recursive function is defined in terms of itself, together with one or more base cases. A correct recursive definition must make progress toward a base case. Recursion is fundamental in mathematical definitions, algorithms, tree processing, and computability theory.
Syntax / Structure / Formula
f(n) = base case, if n is small
f(n) = g(n, f(k)), where k < n
Detailed Explanation
The factorial function is a classic example. The recursive step reduces n to n−1, eventually reaching 0. Recursive definitions can express structures that are difficult to describe iteratively at first.
Example
factorial(0)=1
factorial(n)=n·factorial(n−1)
5! = 5·4·3·2·1 = 120
Output / Result
factorial(5) = 120.
Important Points / Notes
- Every recursion needs a terminating base case for total terminating definitions.
- Recursive calls should move toward the base case.
- Recursion can be converted to iteration using an explicit stack in many cases.
Mini Practice Question
Write a recursive definition for the sum 1+2+...+n.
4.2 Full-Power Computational Mechanisms
Introduction / Definition
A full-power computational mechanism is capable of expressing general effective computation rather than only a restricted class such as regular or context-free computation. General-purpose mechanisms can simulate algorithms involving arbitrary amounts of memory and repeated computation. Turing machines and equivalent models are standard examples.
Syntax / Structure / Formula
- General computation: input → sequence of configurations → output/termination
Detailed Explanation
A full-power mechanism can implement arithmetic, loops, recursion, data manipulation, and simulations. The key feature is not speed but expressive computational power.
Example
Algorithm:
while n > 0:
output(n)
n = n - 1
Output / Result
The mechanism can execute an arbitrary number of iterations depending on the input, subject to termination.
Important Points / Notes
- General computation requires unbounded resources in the theoretical model.
- Turing machines are a canonical model.
- Expressive power and efficiency are different questions.
Mini Practice Question
Why is a Turing machine considered more powerful than a finite automaton?