Search

Search
WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release
of Delphi (Delphi
10.1 Berlin Starter Edition ). There
are a few restrictions, but it is a welcome step toward making
more programmers aware of the joys of Delphi. They do say
"Offer may be withdrawn at any time", so don't delay if you want
to check it out. Please use the
feedback
link to let me know if the link stops working.

Support DFF - Shop

If you shop at Amazon anyway, consider using
this link. We receive a few cents from each purchase.
Thanks.

Support DFF - Donate

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.

Contact

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

Search DelphiForFun.org only

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: