<< Prev        Next >>

ID : 3536

Designate an Array as Procedure Argument

You can designate an array as a procedure argument.

Procedure "SJoin" in example 1 below requests Array (ccc(2)) which has 3 elements. PrintDbg SJoin(aaa, "-") called from Main procedure passes Array aaa as argument defined with 3 elements.

Example 1

Sub Main
	Dim aaa(2) As String
	aaa(0) = "Tom"
	aaa(1) = "Jessica"
	aaa(2) = "George"
	PrintDbg SJoin(aaa, "-")
End Sub

Function SJoin(ByVal ccc(2) As String, ByVal Separator As String) As String
	Dim ddd As String
	ddd = ""
	SJoin = ccc(0) & Separator & ccc(1) & Separator & ccc(2)  
End Function

Array whose Elements Undetermined

You can designate an array whose elements undetermined as a procedure argument.

Function AAA(ByVal aaa() As Integer) As String

Argument "aaa()" in the above example shows integer type array and the number of elements and dimensions are not determined. Describe a process based on the number of elements of argument passed.

Procedure "RateB" in the following example requests array variable "bbb()" whose element number is undetermined. Procedure "Main" passes "aaa(1000)" when retrieving procedure "RateB."

'!TITLE "Display a number ratio of multiple of n within the array"

Sub Main
	Dim aaa(1000) As integer
	Dim n As Integer
	For n = LBound(aaa) To UBound(aaa)
		aaa(n) = 7 * (n + 1) 
	Next
	PrintDbg RateB(aaa, 3) * 100 & "%" 
End Sub

Function RateB(ByVal bbb() as Integer, ByVal k As Integer) As Single
	Dim n As Integer
	Dim m As Integer
	m = 0
	For n = LBound(bbb) To UBound(bbb)
		If bbb(n) Mod k = 0 Then
			m = m + 1
		End If
	Next
	RateB = m / (Ubound(bbb) - Lbound(bbb) + 1)
End Function

Summary of Procedure Argument

Same type of data needs to be assigned to procedure argument. You need to assign a same type of data or designate automatic data type conversion (casting)-enabled data.

Array cannot be automatically converted, so the same type of data needs to be passed.

 

Caller
Array Single number
Array whose elements determined Array whose elements undetermined
Call target Array Array whose elements determined OK when the number of elements and dimensions are the same. NG

NG

Array whose elements undetermined OK OK NG
Single number NG NG OK

Dim declaration cannot define an array whose elements are undetermined.

ID : 3536

<< Prev        Next >>