简体   繁体   中英

How to choose specific element from array?

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

在此处输入图像描述

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:

在此处输入图像描述

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM