Results 1 to 6 of 6

Thread: Pass array to subroutine

  1. #1

    Pass array to subroutine

    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.

  2. #2
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    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.
    Last edited by ReneMiner; 22-03-2013 at 18:54.
    I think there are missing some Forum-sections as beta-testing and support

  3. #3
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,530
    Rep Power
    171
    best ideas always come too late...


    Simplest way is this

    ' inside sub...
    local dummyArray(XDims, YDims) as myType At Pointer
    
    I think there are missing some Forum-sections as beta-testing and support

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5

    Response 2D array to subroutine

    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....

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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
    Last edited by Petr Schreiber; 23-03-2013 at 10:06.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. with new power UDTs, pass byref or byval
    By kryton9 in forum thinBasic General
    Replies: 2
    Last Post: 15-12-2007, 22:57

Members who have read this thread: 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •