' FB 1.05.0 Win64 Type FuncType As Function(As Double) As Double ' These 'shared' variables are available to all functions defined below Dim Shared p As UInteger Dim Shared list() As Double Function sma(n As Double) As Double Redim Preserve list(0 To UBound(list) + 1) list(UBound(list)) = n Dim start As Integer = 0 Dim length As Integer = UBound(list) + 1 If length > p Then start = UBound(list) - p + 1 length = p End If Dim sum As Double = 0.0 For i As Integer = start To UBound(list) sum += list(i) Next Return sum / length End Function Function initSma(period As Uinteger) As FuncType p = period Erase list '' ensure the array is empty on each initialization Return @sma End Function Dim As FuncType ma = initSma(3) Print "Period = "; p Print For i As Integer = 0 To 9 Print "Add"; i; " => moving average ="; ma(i) Next Print ma = initSma(5) Print "Period = "; p Print For i As Integer = 9 To 0 Step -1 Print "Add"; i; " => moving average ="; ma(i) Next Print Print "Press any key to quit" Sleep