Possible to pass an array to a function?
BlitzMax Forums/BlitzMax Beginners Area/Possible to pass an array to a function?
| ||
Can't get this to work. Something like:function Myfunc(empty:int[],a:int,b:int,c:int) 'put some numbers in the array Return full[] end function Just hypothetical stuff....could return multiple results this way. |
| ||
Like this?Function Myfunc:Int[](empty:Int[],a:Int,b:Int,c:Int) 'put some numbers in the array Local full:Int[]=[a,b,c] Return full End Function |
| ||
Well....you're not passing any array values TO the function from an array. I mean more like first you create an array: Local MyArray:int[5,5] Then send the whole array to a function. Think it's possible still tho....brb. |
| ||
Guess you could pass a pointer to an array... |
| ||
Well maybe not... Forgot what I was actually trying to do LOL. Now I'm just playing with Arrays and trying to pass them to a function...do some stuff to em...and pass em back out. =p |
| ||
Strict Local myArray:Int[] = [0, 1, 2, 3, 4, 5] TimesTen(myArray) For Local n = EachIn myArray Print n Next End Function TimesTen(array:Int[]) For Local n = 0 Until array.length array[n] :* 10 Next End Function?? |
| ||
Ok here's the problem. I'm trying to pass:Local myArray:int[5,5] to and from a function. Having no luck here atm. |
| ||
Getting "Unable to Convert From Int Array to Int Array" with every method. Don't think sending multi-dimensional arrays is possible. Thanks for the efforts so far guys. |
| ||
Local myArray:Int[5,5] myFunc(myArray, 1, 2, 3, 4, 5) For Local a:Int = EachIn myArray Print a Next Function myFunc(ar:Int[,] Var, a:Int, b:Int, c:Int, d:Int, e:Int) ar[0,0] = a ar[0,1] = b ar[0,2] = c ar[0,3] = d ar[0,4] = e For Local x:Int = 1 To 4 For Local y:Int = 0 To 4 ar[x,y] = ar[x-1,y]*2 Next Next End Function |
| ||
SuperStrict Function FutzWithArray(arrayToFutzWith:Int[] Var, valueToAdd:Int, anotherValueToAdd:Int) 'put some numbers in the array arrayToFutzWith = arrayToFutzWith[..(arrayToFutzWith.length + 2)] arrayToFutzWith[(arrayToFutzWith.length - 2)] = valueToAdd arrayToFutzWith[(arrayToFutzWith.length - 1)] = anotherValueToAdd End Function Function PrintArray(arrayToPrint:Int[]) Local printString$="Array contents:", i:Int For i = 0 To arrayToPrint.length - 1 printString$ = printString$ + " " + arrayToPrint[i] Next Print printString$ End Function Local myArray:Int[] myArray = [5, 5] PrintArray(myArray) FutzWithArray(myArray, 6, 7) PrintArray(myArray) |
| ||
Ok here's what I was trying to do. Finally got it working successfully. Thanks for helping me solve this. Basically wanted to send a multi-dimensional array to a function, do calcs on it, and return it. Not sure how significant this code is, but it could help some when you need to pass a multi-dimensional array to a dll call. Be aware of the lone commas in the brackets. They gotta be there. >< Here's the code: Print 'Make array1 and fill it with values Local array1:Int[2,2] For b=0 To 1 For a=0 To 1 array1[a,b]=a Print "array1 "+a+","+b+" = "+array1[a,b] Next Next Print 'Make array2 Local array2:Int[,] 'Pass array1 to the function and get array1 back and put it in array2 array2 = ArrayFunc(array1) Print For b=0 To 1 For a = 0 To 1 Print "Back From ArrayFunc "+a+","+b+" = "+array2[a,b] Next Next End Function ArrayFunc[,](this:Int[,]) For b=0 To this.length/2-1 For a=0 To this.length/2-1 Print "Inside ArrayFunc "+a+","+b+" = "+this[a,b] this[a,b]:+5 Next Next Return this End Function |
| ||
You could use an array of arrays ([][] instead of [,]), as multi-dimensional arrays can't be resized/sliced. |