You can edit almost every page by Creating an account and confirming your email.

Recursion termination: Difference between revisions

From EverybodyWiki Bios & Wiki
WikiMasterBot2 (talk | contribs)
m Remove Afc Messages and templates
WikiMasterBot2 (talk | contribs)
m automatic correction by IA
 
Line 9: Line 9:


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


1. If n is 0, returns 0.<br>
1. If n is 0, returns 0.<br>
Line 15: Line 15:
3. Otherwise, return [fibonacci(n-1) + fibonacci(n-2)]
3. Otherwise, return [fibonacci(n-1) + fibonacci(n-2)]


This recursive function [[termination (computer science)|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.
This recursive function [[termination (computer science)|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.


If we look at an example of this in Python we can see what's going on a bit more clearly:
If we look at an example of this in Python we can see what's going on a bit more clearly:
Line 46: Line 46:
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 since we have our base case where we know that the factorial of 0 is 1 (or 0! = 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 since we have our base case where we know that the factorial of 0 is 1 (or 0! = 1).


In the C programing language we could similarly do something like this:
In the C programming language we could similarly do something like this:
<pre>
<pre>
long factorial(int n)
long factorial(int n)

Latest revision as of 23:17, 7 August 2025

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

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.

If we look at an example of this in Python we can see what's going on a bit more clearly:

def fibonacci (n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    elif n > 1:
        return fibonacci(n-1) + fibonacci(n-2)

Factorial Example

An example in the programming language C++:[2]

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 since we have our base case where we know that the factorial of 0 is 1 (or 0! = 1).

In the C programming language we could similarly do something like this:

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

References

  1. ↑ What On Earth is Recursion?
  2. ↑ An Introduction to the Imperative Part of C++. Search this book on


This article "Recursion termination" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:Recursion termination. Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.