Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass an Array to a Sub
#1
Hello all,

I'm pretty new to ZX Basic so please forgive my possible beginner questions.

I'd like to pass an Array to a Sub but cannot seem to figure out how.


Code:
dim SortArray(10) as string

SUB SelectionSort (SortArray) 'Compile Error: Syntax Error. Unexpected token 'SortArray' <ARRAY_ID>

  {Code}

END sub


'************************ Main ****************************

Let SortArray(0) = "Line 00"
Let SortArray(1) = "Line 02"
Let SortArray(2) = "Line 08"
Let SortArray(3) = "Line 06"
Let SortArray(4) = "Line 01"
Let SortArray(5) = "Line 03"
Let SortArray(6) = "Line 07"
Let SortArray(7) = "Line 04"
Let SortArray(8) = "Line 05"

SelectionSort(SortArray) ' Compile Error: Variable 'SortArray' is an array and cannot be used in this context

Thanks in advance for any insights Smile
Reply
#2
Sorry, my example was unclear.
I'd like to pass an array to the routine to be able to re-use it in the same application and also to be able to put it in a library.

So, perhaps I should have presented it like this:



Code:
dim MyArray(10) as string

SUB SelectionSort (SortArray)

  {Code}

END sub


'************************ Main ****************************

Let MytArray(0) = "Line 00"
Let MytArray(1) = "Line 02"
Let MytArray(2) = "Line 08"
Let MytArray(3) = "Line 06"
Let MytArray(4) = "Line 01"
Let MytArray(5) = "Line 03"
Let MytArray(6) = "Line 07"
Let MytArray(7) = "Line 04"
Let MytArray(8) = "Line 05"

SelectionSort(MytArray)
Reply
#3
(04-04-2020, 08:26 AM)XoRRoX Wrote: Sorry, my example was unclear.
I'd like to pass an array to the routine to be able to re-use it in the same application and also to be able to put it in a library.

So, perhaps I should have presented it like this:



Code:
dim MyArray(10) as string

SUB SelectionSort (SortArray)

  {Code}

END sub


'************************ Main ****************************

Let MytArray(0) = "Line 00"
Let MytArray(1) = "Line 02"
Let MytArray(2) = "Line 08"
Let MytArray(3) = "Line 06"
Let MytArray(4) = "Line 01"
Let MytArray(5) = "Line 03"
Let MytArray(6) = "Line 07"
Let MytArray(7) = "Line 04"
Let MytArray(8) = "Line 05"

SelectionSort(MytArray)

ZX Basic does not support (yet) passing arrays by parameter. It's planned, however, but not easy to implement.
Some people use macros (#define ...., like in C, which are allowed) or use global variables for the moment.

I will update when it's implemented.
Reply
#4
Cool - thanks Smile
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)