John
John

Reputation: 1947

How to choose specific element from array?

Let's consider code following which creates array in a way following :

enter image description here

Function my_fun(a As Double, b As Double, n)

    Dim arr() As Variant
    ReDim arr(n - 1)
    Dim i As Integer

    arr(0) = 0
    arr(1) = 1
    For i = 2 To n - 1
        arr(i) = a * arr(i - 1) + b * arr(i - 2)
    Next i
    
    my_fun = Application.Transpose(arr)
    
End Function

This code returns array in which next elements are next sequence values. How can I make my function to return last element of that array instead of just array ? For example :

enter image description here

In this case code should return 4, because it's the very last element of the array. Do you know how it can be performed ?

Upvotes: 0

Views: 103

Answers (1)

BigBen
BigBen

Reputation: 50006

To return the last element of the array:

my_fun = arr(Ubound(arr))

or since the upper bound is n - 1:

my_fun = arr(n - 1)

Upvotes: 2

Related Questions