Category:Recursion: Difference between revisions

added Encyclopedia tag
(simple vs. mutual recursion.)
(added Encyclopedia tag)
Line 1:
[[Category:Solutions by Programming Task]][[Category:Encyclopedia]]'''Recursion''' is the idea that a function can come to an answer by repeatedly calling itself with new arguments until a "base case" or "end condition" is met. One simple example is a [[factorial function]] (for positive integers only). The base case for factorial is "0!" (some people like to use 1 or 2, but 0 is OK for instructional purposes). When 5 is sent as an argument to a recursive factorial function, the function does not know the answer right away. All it knows is that 5! = 5 * 4!. So it calls itself to find out what 4! is. This process continues until it gets to 0!, which is 1. Now it has built up a train of answers: 5! = 5 * 4! = 5 * 4 * 3! etc., and it can find the final answer. Other common examples include [[tree traversal]] and the [[wp:Minimax|min/max algorithm]]. Anyone who can convert the likes of 12,345 into "twelve thousand, three hundred and forty-five" is engaging in the recursive use of language.
 
A pseudocode function to demonstrate recursion would look something like this:
1,150

edits