PlayBASIC V1.64k Beta 12b - Returning Local Arrays from Functions (Maybe).
This afternoon I've been continuing my work on the Function Parser. Now that most of the big issues are resolved, it should be possible to export a locally created array (an array dimmed inside a function), and return and assign it to another array(). Those with a keen eye will no doubt see why I was messing around with Array() operators a few days back

- Anyway, while it doesn't currently work, ideally this could mean that you can dynamically create arrays.
However, there's a catch (isn't there always), exported arrays will be returned as their underlying bank handles, these are use plain integer values. Since the return isn't strongly typed (for flexibility here), it's going to be possible for you to leak arrays if you're not careful.
Anyway, here's the current WIP snippet. So far this parses correctly, but doesn't generate the runtime code for the array exporting.
Dim Table(0)
; Table() = MakeTable(Size)
print "Yep, i'm not dead"
Sync
waitkey
Function MakeTable(Size)
// Declare and create our local array. You'll need to DIM this manually
Dim LocalArray(Size)
EndFunction LocalArray() ; Export local array from function
PlayBASIC V1.64k Beta 12c - Returning Local Arrays from Functions (continued).
Well, we can tick off another feature. I thought this might take a little longer to get working, but it's really gone fairly seamlessly this time. The implementation isn't complete mind you (only currently supports return integer arrays) but the frame work is in now working. Hopefully, there's no hidden drama's awaiting the implementation of the other array types. But, anyway..
So here's a basic functionality example... Not too exciting, but it shows the concept.
Dim Table(0)
// show the initial array state
print ConvertArrayToString(Table())
// Make and return a array 15 elements in size.. The returned array
// is written back into the Table() array. When you make and assignment
// like this, it first undims the data in the destination array, then
// stores the arrays bank in the dest.
Table()=MakeTable(15)
// show the initial array state
print ConvertArrayToString(Table())
// Make and return a array 25 elements in size.. The returned array
// is written back into the Table() array.
Table()=MakeTable(25)
print ConvertArrayToString(Table())
Sync
waitkey
Function MakeTable(Size)
// Declare and create our local array. You'll need to DIM this manually
Dim LocalArray(Size)
for lp =0 to size
LocalArray(lp)=rnd(50)
next
EndFunction LocalArray() ; Export local array from function
Function ConvertArrayToString(Me())
for lp=0 to getarrayelements(Me(),1)
s$=s$+digits$(me(lp),2)+","
next
s$=trimright$(S$,",")
EndFunction s$
Outputs..
00
19,14,06,19,44,17,35,47,49,47,48,19,33,22,33,01
07,45,45,46,39,22,29,20,33,46,01,45,06,46,37,06,00,46,05,50,49,40,31,40,00,03
Here's another example. This one is using a 1D array, to store 1D array handles in. So we're
conceptually creating functionality to resembles the 'arrays within arrays'. To access the dynamically created arrays here we're using a manually declared redirection array via MakeArray. So before we can access any array handles within TABLE() we need to, copy the array bank into our redirect array. Once that's done, the array appears like any other.
setfps 100
// ----------------------------------
// Conceptual Array of Arrays
// ----------------------------------
Dim Table(100)
// make a redirection array. We'll use this it access the 'array banks' stored
// with the TABLE() array. We need this in this example since, Table is just
// an integer array.
MakeArray ints()
EndTime=timer()+1000*10
do
Cls 0
if spacekey() or (rnd(100)>20) and (Timer()<EndTime)
Index=GetFreeCell(Table())
Table(Index)=MakeTable(rndRange(10,20))
; Flushkeys
endif
// press enter to randomly create an array and then store it's bank
// table() array. So we're making an 'lose' associate
if EnterKey()=true or (rnd(100)>10)
//
index=Rndrange(1,GetArrayElements(Table(),1))
if Table(Index)
ints()=Table(index)
Table(index) = 0 ;clear this handle from the array we used to remember them in
Ints()=0 ; free this array buffer
; Flushkeys
endif
endif
// Display the arrays
for Index=1 to getarrayelements(Table(),1)
if Table(Index)
ints()=Table(index)
print ConvertArrayToString(Ints())
endif
next
Sync
loop
Sync
waitkey
Function MakeTable(Size)
// Declare and create our local array. You'll need to DIM this manually
Dim LocalArray(Size)
// Fill it full of stuff
for lp =0 to size
LocalArray(lp)=rnd(50)
next
EndFunction LocalArray() ; Export local array from function
// Convert array to
Function ConvertArrayToString(Me())
for lp=0 to getarrayelements(Me(),1)
s$=s$+digits$(me(lp),2)+","
next
s$=trimright$(S$,",")
EndFunction s$
Download
You'll find Beta 12c posted on our forums as always...