Image Map Image Map
Results 1 to 3 of 3

Thread: Fibonacci Numbers with Turbo Pascal 3.

  1. #1
    Join Date
    May 2003
    Location
    Back of Burke (Guday!), Australia
    Posts
    2,861

    Default Fibonacci Numbers with Turbo Pascal 3.

    Hello,

    I've stumbled across an unusual problem with this following program while using Turbo Pascal 3:


    Code:
    program fibonacci;
    
    function fib(n:integer): integer;
    begin
        if (n <= 2) then
            fib := 1
        else
            fib := fib(n-1) + fib(n-2);
    end;
    
    var
        i:integer;
    
    begin
        for i := 1 to 16 do
            write(fib(i), ', ');
        writeln('...');
    end.

    The problem being the correct sequence of numbers isn't being produced. It's starts fine with 1, 1, but then it counts up to 15. The code is on Progopedia and includes Turbo Pascal 3 as part of the compilers which can compile. I'm using the CP/M version which has been tweaked for the Amstrad CPC computers, so I'm assuming it works correctly for the 16bit versions, but don't know if other CP/M versions of TP3 are struggling with this?

  2. #2

    Default

    This I found on the web:

    CP/M-80 version only: Because of the way local variables
    are handled during recursion, a variable local to a
    subprogram must not be passed as a var-parameter in
    recursive calls.
    There is more info at:

    https://groups.google.com/forum/#!to...al/ifEsKpb6Pqo

  3. #3
    Join Date
    May 2003
    Location
    Back of Burke (Guday!), Australia
    Posts
    2,861

    Default

    Thanks for that, it's a wonder I haven't come across that limitation earlier.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •