home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TP_ADV.ZIP / LIST0202.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-07-31  |  894 b   |  35 lines

  1. {$N+,E+}
  2. Program Fibonacci;
  3. { This program will calculate the NumToCalc Fibonacci     }
  4. { number.  It is intended as an example of using the call }
  5. { stack to examine the currently active procedures        }
  6.  
  7. Const
  8.   NumToCalc = 10;       { The specific Fib num to calc    }
  9.  
  10. Function Fib(Num : Integer) : Extended;
  11. { Recursive function to calulate a Fibonacci number. This }
  12. { function will be used to show how to use the call stack }
  13. { feature.                                                }
  14. Var
  15.   Fib1, Fib2 : Extended; { Local vars for calculation      }
  16.  
  17. Begin
  18.   If Num = 0 Then
  19.     Fib := 0.0
  20.   Else
  21.     If Num = 1 Then
  22.       Fib := 1.0
  23.     Else
  24.     Begin
  25.       Fib1 := Fib( Num - 1 );
  26.       Fib2 := Fib( Num - 2 );
  27.       Fib := Fib1 + Fib2;
  28.     End;
  29. End;
  30.  
  31. Begin
  32.   Writeln( NumToCalc,' Fibonacci Number is ',
  33.            Fib( NumToCalc ):20:10 );
  34. End.
  35.