How pass an array to a function ?
BlitzMax Forums/BlitzMax Beginners Area/How pass an array to a function ?
| ||
how can i pass an multidimensional array in a function ? example: Global Pattern: Byte[7, 5] function ModPattern: byte(aPattern[]: byte) ' ..... some code here ... end function |
| ||
TryFunction ModPattern(aPattern:Array)Incidently, there's no compelling reason to pass something with Global visibility to a Function. |
| ||
Well, if you also wanted to use locals as a parameter, there would be. Or you want to use it on multiple Global arrays. |
| ||
Cam somebody expand on this for me please. I want to pass 2 local arrays to the same function and return a single array <edit> Bit more info. I'd like to put this into a function... Local a[2] Local b[2] a[0]=0 a[1]=1 b[0]=2 b[1]=3 Local c[] For n=EachIn a Print n Print "num : " + num c=c[..num+1] c[num]=n num:+1 Next For n=EachIn b Print "num : " + num c=c[..num+1] c[num]=n num:+1 Next For o = EachIn c Print o Next I've tried using slices to copy the 2 arrays into a 3rd and also the listtoarray by creating a new list from the array list but without success. If there's an easy way I have missed then please let me know. |
| ||
I want to pass 2 local arrays to the same function and return a single array Can't test it right now, but I would assume something likeFunction myArrayThing:Array(array1:Array, array2:Array)Should do the trick. At least if you're trying to do what I think you're trying to do. |
| ||
Thanks for the quick response FlameDuck. I have that as my function but I'm struggling on calling the function... Local c:array[]=myArrayThing:array(a:array,b:array) or many permutations of that. |
| ||
To pass a (2d) array to a function, use something like:Function ModPattern( aPattern:Byte[,] ) 'blah... End Function Local myPattern:Byte[10,10] ModPattern myPattern |
| ||
... and sending 2 single dimension arrays with a third array returned? |
| ||
At home now, and this seems to be working:Function myArrayThing:Array(array1[], array2[]) Local result[array1.length + array2.length] Local c,i For i=EachIn array1 result[c] = i c:+1 Next For i=EachIn array2 result[c] = i c:+1 Next Return result End Function Local a:Int[] = [1,2,3,4] Local b:Int[] = [5,6,7,8] Local c:Int[] = myArrayThing(a,b) For i=EachIn c Print i Nextalthough I can't offer an explaination as to why Array <> []. |
| ||
many many thanks. I'd tried so many things in so many permutations I'd lost the thread of what I was trying to do. Thanks again. |