PDA

View Full Version : Pass array to subroutine



Barbara
22-03-2013, 17:00
How do I pass a dimensioned two-dimensional array to a subroutine? I have long-time programming experience with Fortran, C, and PL/SQL, but nothing "program-language-like" I've tried works producing sort-of non-helpful errors e.g. parentheses missing, variable not found. My work-around was to write the array to a file and include the file in the subroutine - this is tackiness which is not my preference.

ReneMiner
22-03-2013, 18:28
You might pass a pointer to the adress of the first array-element and also the Ubounds of each Dimension to the Sub/Function like



Type myType
foo as Integer
bar as Long
End Type

Dim my2DArray(12,34) as myType

SomeSub(Varptr(my2DArray(1,1)), 12, 34)
' ...

Sub SomeSub(Byval Pointer as Dword, byval XDims as Long, byval YDims as Long)

' inside the Sub you know from the dimensions
' how to multiply to get the position of the element
' and also the upper bounds

' if you want to access some element

' as my2DArray(7,5)


Local pPosition as Dword = Pointer + ((7 * XDims + 5 ) - 1) * SizeOf(myType)

' why -1 ?
' because Pointer is already at Element 1,1, so subtract it

Local XX as myType At pPosition

' XX gives you access to element (7,5) of my2DArray inside this sub
XX.foo = 123
if XX.bar then '....

End Sub


But that will only work if no dynamic strings inside myType.

ReneMiner
22-03-2013, 18:54
best ideas always come too late...


Simplest way is this




' inside sub...
local dummyArray(XDims, YDims) as myType At Pointer

Petr Schreiber
22-03-2013, 19:45
Hi,

passing the two dimensional array to function is quite simple, please see this example:


Function TBMain()

Dim nValues(10, 15) As Long

myFunc(nValues)

MsgBox 0, nValues(1, 1) ' -- Will show 5

End Function

' -- If you pass array, do not forget the () in the parameter name
Function myFunc( ByRef myArray() As Long )

MsgBox 0, "Array is " + UBound(myArray, 1) + "x" + UBound(myArray, 2)

myArray(1, 1) = 5

End Function



Petr

Barbara
22-03-2013, 20:16
Based on the responses I'm sure I'll get my programming working. I assume I CALL the subroutine with a parameter as an array with Call MySub(abcd()) rather then Call MySub(30,15). Thanks....

Petr Schreiber
23-03-2013, 10:02
Hi Barbara,

in fact, you dont need to use CALL keyword at all in this case, and you also do not use the () when passing array:


DIM nValues(...) As Number

...

myFunc( nValues() ) ' -- This will not work, you don't have to specify extra "()" when calling

myFunc( nValues(1) ) ' -- This will pass just the first element of 1D array - for function with signature FUNCTION myFunc( param1 As Number )

myFunc( nValues(1, 1) ) ' -- This will pass just the first element of 2D array - for function with signature FUNCTION myFunc( param1 As Number )

myFunc( nValues ) ' -- This is correct way to pass whole array - for function with signature FUNCTION myFunc( param() As Number )


myFunc - name of function
nValues - name of array

So yes, you can pass whole array into routine.


Petr