[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
recursion
In this example we see two things consistently present in each recursive function: a trivial solution case, and the part of the code that simplifies the solution until it is expressed by trivial solutions.
The trivial solution case is here expressed by the if-then part of the code. The first two numbers of the Fibonacci sequence are both 1.
The else part of the code shows how the solution for any sequential element of the Fibonacci sequence can be expressed by the sum of the previous two numbers.
For a simpler explanation of what recursion is, please see recursion :)
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
recursion
(Computer Science) Recursion
Recursion is an algorithmic approach to solving problems by simplifying the problem until the solution is trivial. For instance, consider the following C/C++ function that calculates an element in the Fibonacci sequence:
int fibonacci(int n)
{
if (1==n || 2==n)
{
return 1;
}
else
{
return fibonacci(n-2) + fibonacci(n-1);
}
}
In this example we see two things consistently present in each recursive function: a trivial solution case, and the part of the code that simplifies the solution until it is expressed by trivial solutions.
The trivial solution case is here expressed by the if-then part of the code. The first two numbers of the Fibonacci sequence are both 1.
The else part of the code shows how the solution for any sequential element of the Fibonacci sequence can be expressed by the sum of the previous two numbers.
For a simpler explanation of what recursion is, please see recursion :)
'Recursion' links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
