Passing and returning arrays

BlitzMax Forums/BlitzMax Programming/Passing and returning arrays

Hi, so far in BMax I haven't used arrays much (I have plenty in other languages) but now I'm wondering what the various syntaxs are for the following:

1) passing an array into a function. i.e. how to call the function and how to declare the function. I assume an array will be passed as a variable automatically without me having to put "var" after it?
2) returning a new array from a function. i.e. is this even possible, like say I declare an array of a certain size (or no size?) and I call a function and assign the return value, which is an array, to the array that I declared.

Maybe I should just stick to TLists ;-)

Thanks for any help.

To pass an array:
function passAnArray(myArray:float[])
   local theSizeIWant:int = 3
   if myArray.length <> theSizeIWant then
        return 
   end if
end function


To return an array:
function returnArray:float[]()
   local retArray:float[3]
   return retArray
end function


of course float is just the datatype, can be whatever.

Thanks Alex. seem to have figured it out:

Function ArrayFiddler:String[](In$[])
   Local out$[]
   return out
End Function

Local myarray$[]
myarray = ArrayFiddler(myarray)


Now I know I could just modify the In array in the function and because it's passed as a var (isn't it?) it would remain modified after the function, but my function is pretty complex and it's easier to make a new array and pass that out.

Anyway another question arise. Will BMax garbage collection handle this line:

myarray = ArrayFiddler(myarray)

properly because the original myarray is now being overwritten, all the strings in the original one need to be freed and so does the original array. Will that be done? I hope so...

some test code that shows arrays ARE passed as var (of course). Interestingly the compiler lets you redundantly write var after the array in the function parameters...

Strict
Local test$[2]

test[0] = "hello"

fiddle(test)

Print test[0]

Function fiddle(in$[])
	in[0] = "goodbye"
End Function



...because the original myarray is now being overwritten


Blitzmax handles arrays as special objects from what I understand. You aren't really 'overwriting' it technically. You're just changing what myarray points to. So the original array no longer has a reference to it...and therefore gets garbage collected.

Thanks.

yep thought so. It handles them as objects but we can't as developers (i.e. no Extending allow, shame).

I just bunged this round the function call:

		GCCollect
		Local old = GCMemAlloced()
                myarray = ArrayFiddler(myarray)
		GCCollect
		Notify old + "  " + GCMemAlloced()



And it's the same, which confirms it.


Maybe I should just stick to TLists



Well yes TLists are designed for games in mind really aren't they? But if you have data that doesn't change arrays are certainly faster.

TLists are just more OOP I feel and less techie and better for changing data for sure you are right.

Interestingly the compiler lets you redundantly write var after the array in the function parameters...
Not quite redundant...
Local test[10]

Print test.length

ResizeMe1(test, 20)

Print test.length

ResizeMe2(test, 30)

Print test.length

End

Function ResizeMe1(array[], size)
	array = array[..size]
End Function
	  
Function ResizeMe2(array[] Var, size)
	array = array[..size]
End Function


Yan: What the hell! so you can change the contents of array slots without var but not the size, well that is worth knowing. Thanks.