Recursion termination

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Flyer22 Frozen (talk | contribs) at 18:02, 13 January 2014 (Reverted 1 good faith edit by 2001:630:D0:F111:489D:BC1B:4ED6:77F9 using STiki). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computing, Recursion termination is when certain conditions are met and the recursive algorithm ceases calling itself and begins to return values.[1] This happens only if with every recursive call the recursive algorithm changes its state and moves towards the base case. Cases that satisfy the definition without being defined in terms of the definition itself are called base cases. They are small enough to solve directly.[2]

Examples

Fibonacci function

The Fibonacci function(fibonacci(n)), which takes integer n(n >= 0) as input, has three conditions

1. if n is 0, returns 0.
2. if n is 1, returns 1.
3. otherwise, return [fibonacci(n-1) + fibonacci(n-2)]

This recursive function terminates if either conditions 1 or 2 are satisfied. We see that the function's recursive call reduces the value of n(by passing n-1 or n-2 in the function) ensuring that n reaches either condition 1 or 2.

C++

C++ Example:[3]

int factorial(int number)  
{
	if (number == 0)
		return 1;
	else
		return (number * factorial(number - 1)); 
}

Here we see that in the recursive call, the number passed in the recursive step is reduced by 1. This again ensures that the number will at some point reduce to 0 which in turn terminates the recursive algorithm.

References

  1. ^ http://www.itl.nist.gov/div897/sqg/dads/HTML/recursiontrm.html
  2. ^ Recursion Lecture, Introduction to Computer Science pg. 3 (PDF).
  3. ^ An Introduction to the Imperative Part of C++.

External links