[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
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





last edited (December 4, 2006) by bilderbikkel, Number of views: 3312, Current Rev: 4 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.