Domino Code Fragment

Code Name*
Fibonacci Sequence of n using recursion
Date*
04/28/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.3.17.150.89
Description*
Returns F ibonacci Sequence of n using recursion
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
A function that calls itself is known as a recursive function. Each time a recursive function calls itself, an entirely new data area for that particular call must be allocated. Writing a recursive function produces compact code but causes an increase in the use of memory. Each call causes a new data area to be allocated, and each reference to an item in the function's data area is to the data area of the most recent call. Similarly, each return causes the current data area to be freed, and the data area allocated immediately prior to the current area becomes current. If you were to run this function with n = 6 then Fibonacci(3) is computed three seperate times. It would be much more efficient to "remember" the value of Fibonacci(3) the first time that it is evaluated and reuse it each time that it is needed. An iterative method of computing Fibonacci(n) such as is found in " Fibonacci Sequence of n using iteration" document is more efficient.
Files/Graphics attachments (if applicable): Code:
The celebrated Fibonacci Sequence (usually denoted by F0,F1,F2,F3,...) is as follows
 0,1,1,2,3,5,8,13,21,34,55,...

That is F0 = 0 and F1 = 1 and each succeeding term is the sum of the two preceding terms.
For example, the next two terms of the sequence are 34 + 55 = 89 and 55 + 89 = 144

The formal definition of this function follows:
(a) If n = 0 or n = 1, then Fn = n.
(b) if n > 1, then Fn = Fn-2 + Fn-1

Public Function Fibonacci (n As Integer) As Long
    Dim x As Integer
    Dim y As Integer
    If n  > 0 Then
         If n <= 1 Then         'End the recursive calling chain.
              Fibonacci = n
              Exit Function
         End If
         x =  Fibonacci (n - 1)
         y =  Fibonacci (n - 2)
         Fibonacci = x + y
    End If
End Function