43 lines
738 B
Plaintext
43 lines
738 B
Plaintext
' FB 1.05.0 Win64
|
|
|
|
#Lang "fblite"
|
|
|
|
Option Gosub '' enables Gosub to be used
|
|
|
|
' Using gosub to simulate a nested function
|
|
Function fib(n As UInteger) As UInteger
|
|
Gosub nestedFib
|
|
Exit Function
|
|
|
|
nestedFib:
|
|
fib = IIf(n < 2, n, fib(n - 1) + fib(n - 2))
|
|
Return
|
|
End Function
|
|
|
|
' This function simulates (rather messily) gosub by using 2 gotos and would therefore work
|
|
' even in the default dialect
|
|
Function fib2(n As UInteger) As UInteger
|
|
Goto nestedFib
|
|
|
|
exitFib:
|
|
Exit Function
|
|
|
|
nestedFib:
|
|
fib2 = IIf(n < 2, n, fib2(n - 1) + fib2(n - 2))
|
|
Goto exitFib
|
|
End Function
|
|
|
|
For i As Integer = 1 To 12
|
|
Print fib(i); " ";
|
|
Next
|
|
|
|
Print
|
|
|
|
For j As Integer = 1 To 12
|
|
Print fib2(j); " ";
|
|
Next
|
|
|
|
Print
|
|
Print "Press any key to quit"
|
|
Sleep
|