Page 1 of 1

Allocating an array and passing it outside a procedure

Posted: Mon Dec 17, 2007 6:06 am
by Mistrel
Here is a neat solution I found for creating arrays inside procedures and then passing them out.

Code: Select all

Procedure MakeList()
	Static Dim Array(25)
	For i=1 To 25
		Array(i)=Random(255)
	Next i
	ProcedureReturn @array()
EndProcedure

Dim NewArray(0): aptr=MakeList()
Swap NewArray(),aptr

For i=1 To 25
	Debug NewArray(i)
Next i
It doesn't work the other way around though.

Code: Select all

Procedure MakeList(*Array)
	Static Dim LocalArray(0)
	Swap LocalArray(),*Array
	Size=PeekL(*Array-8) ; size of array
	Debug Size
	For i=1 To Size
		LocalArray(i)=Random(255)
	Next i
	Swap LocalArray(),*Array
EndProcedure

Dim Array(25)
MakeList(@Array())

For i=1 To 25
	Debug Array(i)
Next i