Shop OBEX P1 Docs P2 Docs Learn Events
Array passing — Parallax Forums

Array passing

mhm21mhm21 Posts: 14
edited 2009-08-29 00:51 in Propeller 1
Hello all! I come to you humbly for a quick complete beginner question. I have been making my way through the tutorials and have a question about passing "arrays" between objects. Namely I am trying to pass two arrays of longs, call them array A and array B to an method that will perform the equivalent of a 3x3 matrix multiplication and return the resulting array. I have the array multiplication down, but am having an amazingly difficult time figuring out how to pass the resulting multiplied array back to the main method. Could someone please point me to either another forum post or give me a basic explanation here? Thanks!

Comments

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-08-28 22:30
    You pass the array with a pointer address using the @ before the value. If the object method just alters the original array, you don't need to pass anything back. If you create a new array in the object's method you should RETURN the new array's pointer address in the same fashion (@). But if you create a new array in the method (after the PUB method_name | array[noparse][[/noparse]10]), as soon as the method returns, the memory space is now free game as stack and variable space. You should make it a more permanent array using the VAR block.
    Hope that helps...it was kind of confusing.

    Some code samples are always a good thing to include to let us help you better.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!
  • Mike GreenMike Green Posts: 23,101
    edited 2009-08-28 22:36
    Arrays are passed by passing the address of the beginning of the array as a value to the method. Within the method, they're referenced by using BYTE, WORD or LONG as follows:
    VAR long a[noparse][[/noparse] 9 ], b[noparse][[/noparse] 9 ], c[noparse][[/noparse] 9 ]
    
    PUB main
       multiply(@a,@b,@c)
    
    PUB multiply(x, y, z) | i, j, k, t
    ' Multiply the 3x3 array y times the 3x3 array z and store the result in the 3x3 array x
       repeat i from 0 to 2
          repeat j from 0 to 2
             t := i * 3 + j
             long[noparse][[/noparse] a ][noparse][[/noparse] t ] := 0
             repeat k from 0 to 2
                long[noparse][[/noparse] a ][noparse][[/noparse] t ] := long[noparse][[/noparse] b ][noparse][[/noparse] i * 3 + k ] * long[noparse][[/noparse] c ][noparse][[/noparse] k * 3 + j ]
    
    
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-08-28 22:40
    ...but if it is in a daughter object, you will have to use some iteration of long[noparse][[/noparse]address + loop_index * 4] or word[noparse][[/noparse]address + loop_index * 2] or byte[noparse][[/noparse]address + loop_index]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!
  • Mike GreenMike Green Posts: 23,101
    edited 2009-08-29 00:51
    Actually, this will work the same in a sub-object.
Sign In or Register to comment.