範例1: 傳遞多資料與接收單一資料型態
'函數端
Function ReturnArrayArt(F1 As Integer,F2 As Integer) As Integer
Dim ArrayQ As Integer
ArrayQ = F1 - F2
ReturnArrayArt = ArrayQ
End Function
'呼叫端
Dim GetValue As Integer
Dim GP As Integer
Dim GD As Integer
GP = 99
GD = 9
GetValue = ReturnArrayArt(GP,GD)
'執行結果:
GetValue = 90
範例2: 傳遞多資料(包含矩陣)與利用矩陣方式接收多資料型態
'函數端
Function ReturnArrayORZ(ByVal AString As String, ByVal ADouble As Double, TArray() As Integer, TStringArray() As String) As String()
'請注意此Function是定義為String(),也就是說回傳直是字串的矩陣
Dim ArrayQ(100) As String
'把Function接收到的數值,指定給ArrayQ()的矩陣內
ArrayQ(0) = AString
ArrayQ(1) = Str(ADouble) '把 Double 轉型為 String
ArrayQ(2) = Str(TArray(0)) '把 Double 轉型為 String
ArrayQ(3) = Str(TArray(1)) '把 Double 轉型為 String
ArrayQ(4) = Str(TArray(2)) '把 Double 轉型為 String
ArrayQ(5) = Str(TArray(3)) '把 Double 轉型為 String
ArrayQ(6) = Str(TArray(4)) '把 Double 轉型為 String
ArrayQ(7) = Str(TArray(5)) '把 Double 轉型為 String
ArrayQ(8) = TStringArray(0, 0)
ArrayQ(9) = TStringArray(0, 1)
ArrayQ(10) = TStringArray(0, 2)
ArrayQ(11) = TStringArray(1, 0)
ArrayQ(12) = TStringArray(1, 1)
ArrayQ(13) = TStringArray(1, 2)
ArrayQ(14) = TStringArray(2, 0)
ArrayQ(15) = TStringArray(2, 1)
ArrayQ(16) = TStringArray(2, 2)
'請注意此Function是定義為String(),同型態才能接收相對應的值,固ArrayQ()
'定義為String
ReturnArrayORZ = ArrayQ
End Function
'呼叫端
Dim TestString As String '定義TestString 為String型態
Dim TestDouble As Double '定義TestDouble 為Double型態
Dim TestArray(5) As Integer '定義TestArray(5) 矩陣為Double型態
Dim TestStringArray(2, 2) As String '定義
TestStringArray(2,2) 矩陣為Double型態
Dim GetValue() As String '定義
GetValue() 矩陣為String型態
'指定數值給 TestString , TestDouble , TestArray() ,TestStringArray( , )
TestString = "ABC"
TestDouble = 3.1415926
TestArray(0) = 90
TestArray(1) = 91
TestArray(2) = 92
TestArray(3) = 93
TestArray(4) = 94
TestArray(5) = 95
TestStringArray(0, 0) = "ㄅ"
TestStringArray(0, 1) = "ㄆ"
TestStringArray(0, 2) = "ㄇ"
TestStringArray(1, 0) = "A"
TestStringArray(1, 1) = "B"
TestStringArray(1, 2) = "C"
TestStringArray(2, 0) = "a"
TestStringArray(2, 1) = "b"
TestStringArray(2, 2) = "c"
'呼叫ReturnArrayORZ函數,並給予其傳送的參數
GetValue = ReturnArrayORZ(TestString, TestDouble, TestArray(), TestStringArray())
'執行結果:
GetValue(0) = ABC
GetValue(1) = 3.1415926
GetValue(2) = 90
GetValue(3) = 91
GetValue(4) = 92
GetValue(5) = 93
GetValue(6) = 94
GetValue(7) = 95
GetValue(8) = ㄅ
GetValue(9) = ㄆ
GetValue(10) = ㄇ
GetValue(11) = A
GetValue(12) = B
GetValue(13) = C
GetValue(14) = a
GetValue(15) = b
GetValue(16) = c
說明:
Function的使用在一般開發程式中,在處理有重複性的計算或處理方式是非常方邊的工具,以上分享了呼叫端與Function間的呼叫方式,多值傳遞,矩陣傳遞,等方式,提供一些範例,希望開發者有更便利的方法可以使用
沒有留言:
張貼留言