Search results
Results From The WOW.Com Content Network
Chains of three or more functions are possible; for example, function 1 calls function 2, function 2 calls function 3, and function 3 calls function 1 again. Indirect recursion is also called mutual recursion , which is a more symmetric term, though this is simply a difference of emphasis, not a different notion.
A classic example of recursion is the definition of the factorial function, given here in Python code: def factorial ( n ): if n > 0 : return n * factorial ( n - 1 ) else : return 1 The function calls itself recursively on a smaller version of the input (n - 1) and multiplies the result of the recursive call by n , until reaching the base case ...
The μ-recursive functions are closely related to primitive recursive functions, and their inductive definition (below) builds upon that of the primitive recursive functions. However, not every total recursive function is a primitive recursive function—the most famous example is the Ackermann function.
The Ackermann function A(m,n) is a well-known example of a total recursive function (in fact, provable total), that is not primitive recursive. There is a characterization of the primitive recursive functions as a subset of the total recursive functions using the Ackermann function.
These examples reduce easily to a single recursive function by inlining the forest function in the tree function, which is commonly done in practice: directly recursive functions that operate on trees sequentially process the value of the node and recurse on the children within one function, rather than dividing these into two separate functions.
A recursive definition of a function defines values of the function for some inputs in terms of the values of the same function for other (usually smaller) inputs. For example, the factorial function n! is defined by the rules ! = (+)! = (+)!.
The primitive recursive functionals are the smallest collection of objects of finite type such that: The constant function f(n) = 0 is a primitive recursive functional; The successor function g(n) = n + 1 is a primitive recursive functional; For any type σ×τ, the functional K(x σ, y τ) = x is a primitive recursive functional
Recursion allows direct implementation of functionality defined by mathematical induction and recursive divide and conquer algorithms. Here is an example of a recursive function in C/C++ to find Fibonacci numbers: