Passing arrays to functions

BlitzPlus Forums/BlitzPlus Programming/Passing arrays to functions

mikek314(Posted 2011) [#1]
Hi, all. I used Blitz Basic a lot many years ago and am now relearning using BlitzPlus. I looked through the docs and this forum and could not find the answer to this.

Is it possible to pass an array (possibly multi-dimensional) to a function? If so, what is the syntax, of both the call to the function and the function declaration?

Thanks in advance,
Mike K


Mainsworthy(Posted 2011) [#2]
Dim myarray(100,100) etc.... its available to all functions, put it at the start of the program.


H&K(Posted 2011) [#3]
I know this isnt what you are asking, but you know that all all arrays in BPlus are Global?

That is you get access to them inside your functions without the need to pass any thing?


RifRaf(Posted 2011) [#4]
I guess if you wanted to pass a certain array you could do something like
this..

TOTALINDEX=10
Dim PassArray(Totalindex,100,100)

Function UseArray(Index)
usearrayvalue=PassArray(index,x,y)
..
.

Or use types and just pass either the type itself or the handle to a function.

Type MyArray
field X[100]
field Y[100]
end type


Yasha(Posted 2011) [#5]
Standard example:

Function SquareArray(a[10])
    Local e
    For e = 1 to 10
        a[e] = a[e] * a[e]
    Next
End Function

Function PrintArray(a[10])
    Local e
    For e = 1 to 10
        Write a[e] + " "
    Next
    Print ""
End Function

Local arr1[10], arr2[10], e

For e = 1 to 10
    arr1[e] = e
    arr2[e] = 11 - e
Next

SquareArray arr1
SquareArray arr2

PrintArray arr1
PrintArray arr2

Waitkey
End


C-style arrays can't be multidimensional and can't be resized.

So in the function declaration, you declare the parameter as a C-style array - with a constant size, which is part of the type (i.e. passing an int array of size 20 to those functions above would be a type error). To pass the array, you just use the name with no index.

Incidentally, zero-sized (one element) local arrays also make excellent out-parameters, as they're just about as fast as local variables when accessed with a constant index of 0. (If you don't know what this means, it's just a fancy - and fast - way to return multiple values using basically the same method as the SquareArray function above).

Last edited 2011


mikek314(Posted 2011) [#6]
Thanks for all the replies, I understand now. The key point I was missing is that "c-style" arrays (for lack of a better term) cannot be multi-dimensional. But I guess when I really need that I can use a custom type.


Stamm(Posted 2011) [#7]
2 ways you could work in C-style multiD arrays: either you use nested types with arrays in them
Type dim2
Field size
Field dat[200]
End Type

Function YourFunc(blah.dim2[200])
for i=0 to 200
for j=0 to 200
print "data at "+i+", "+j+": "blah[i]\dat[200]
End Function

or you could use split 1D-arrays
local array[255],arrayw=16 ;a 16x16 array: 16x16-1=255
;accessing the array data:

Function GetData(x,y) ;x and y range from 0-15.
Return array[ 16*(y-1)+x ]
End Function