Arrays in Blitz Max

BlitzMax Forums/BlitzMax Beginners Area/Arrays in Blitz Max

Kevin_(Posted 2005) [#1]
I know it is possible to pass an entire array to a function but can you return an array from the function as well?

I need to know this before I go ahead and buy BlitzMax so that I can covert all my sorting algorithms etc...


Robert(Posted 2005) [#2]
Yes.

In BlitzMAX arrays are special types of object. Objects are always passed by reference in BlitzMAX, so you don't pass the whole array to the function, nor do you return the whole array, you just pass the location in memory where the array is stored. This is only the size of an integer.

Example (a bit of a silly frivilous one, but it proves the point. The function accepts an input array, produces a new output array from it and returns it).

Framework BRL.Blitz
Import BRL.Basic
Strict

'Method One:

Local myArray:Int[]=[3,43,67,21,98]

Local outputArray:Int[]=modifyArray(myArray)

For Local i:Int = EachIn outputArray
	Print i
Next

Function modifyArray:Int[](inputArray:Int[])
	
	Local output:Int[inputArray.length]
	
	For Local i=0 Until inputArray.length
		output[i]=inputArray[i]+2
	Next
	
	Return output

End Function



Robert(Posted 2005) [#3]
Regarding sorting, I should point out that BlitzMAX has a really nifty TList type built in (BRL.LinkedList module) which has sorting facilities. 'Max arrays also have a sort method:

arrayName.sort


Kevin_(Posted 2005) [#4]
Cheers Robert!