' Recursion — Recursive factorial function
#COMPILE EXE
#DIM ALL

FUNCTION Factorial(n AS LONG) AS QUAD
    IF n <= 1 THEN
        FUNCTION = 1
    ELSE
        FUNCTION = n * Factorial(n - 1)
    END IF
END FUNCTION

FUNCTION Fibonacci(n AS LONG) AS QUAD
    IF n <= 0 THEN
        FUNCTION = 0
    ELSEIF n = 1 THEN
        FUNCTION = 1
    ELSE
        FUNCTION = Fibonacci(n - 1) + Fibonacci(n - 2)
    END IF
END FUNCTION

FUNCTION PBMAIN() AS LONG
    LOCAL i AS LONG

    PRINT "Factorial:"
    FOR i = 0 TO 10
        PRINT "  " & STR$(i) & "! = " & STR$(Factorial(i))
    NEXT i

    PRINT
    PRINT "Fibonacci:"
    FOR i = 0 TO 15
        PRINT "  Fib(" & STR$(i) & ") = " & STR$(Fibonacci(i))
    NEXT i

    FUNCTION = 0
END FUNCTION
