Recursion

[Home]   [Projects]    [Delphi Techniques]   [Math Topics]   [Library]   [Utilities]

 

Available Now

Search

Google
 

Search WWW             

Search delphiforfun.org

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Help support DFF

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

 

Google
 

Search WWW

Search delphiforfun.org

 

 

Recursion

Recursion is a programming technique that can accomplish a lot with a little bit of code.  Because recursive procedures call themselves, their operation seems like magic.   Here are a couple of recursive functions - one to calculate  factorials and one to calculate Fibonacci numbers. 

Example 1: Factorials                    

N factorial for a positive integer N is defined as the product of the integers 1 through N.  It is usually written as N!.   

Function factorial(N:integer):Integer;

Begin

   If N=1 then result:=1 

   else result:=N*factorial(N-1);

end; 

Example 2: Fibonacci Numbers

Fibonacci numbers are members of an infinite series of integers in which the first two numbers are 1, and each number after the 2nd is the sum of the two preceding numbers (1, 1, 2, 3, 5, 8, 13....). Here's a function that returns the Nth Fibonacci number:

  

Function Fibonacci(n:integer) :integer;

Begin

   If N<=2 then result:=1

  else  result:=Fibonacci(n-1)+Fibonacci(n-2);

end;

 

Here is a program that calculates integer powers of a real number.  Recursion is one of 4 techniques tested.  The program will also introduce you to techniques for accurately measuring program run times.  Click here to download source code for IntPowerDemo.

 


  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2008, Gary Darby    All rights reserved.