How to pass a list to a subroutine in BASIC-256

423 Views Asked by At

I am having difficulty passing a list to a subroutine (embarrassment); I get a compiler error. I have followed the BASIC-256 documentation on arrays (http://doc.basic256.org/doku.php?id=en:arrays) and included the [] brackets in the subroutine argument as required:

subroutine print_list(list) # or subroutine print_list(list[]) <----- **compiler** error occurs here
   for element=0 to list[?]-1
      print list[element]
   next element
end subroutine

subroutine main()
   list = {5.9, 6.0, 5.9, 5.7, 5.6, 5.7}
   call print_list(list[]) #  <----- **compiler** error occurs here
end subroutine

call main()

The compiler complains that I have an error in the subroutine call argument part.

I have tried fixing this by; (i) checking my initialization of the list; (ii) checking the subroutine definition and parameters (compiler doesn't like the square brackets there either); (iii) excluding the [] brackets from the argument and (iv) tried contacting the BASIC-256 Facebook page.

Thank you for your time....

3

There are 3 best solutions below

1
On BEST ANSWER

Based on the info on the site, you cannot pass arrays to your own subroutines, only internal (built-in) ones.

If the variables are global in nature, you're fine, just perform whatever actions you want on the array, but if they needed to be local, it can't be done with this particular variation of BASIC.

0
On

Having read the documentation thoroughly there is a function called 'ref' that allows you to pass a variable or array by reference to a function or subroutine. Doing this allows me to print the array content.

Here's my previous code re-written to pass the array by reference:

subroutine print_list(list)
   for element=0 to list[?]-1
      print list[element]
   next element
end subroutine

subroutine main()
   list = {5.9, 6.0, 5.9, 5.7, 5.6, 5.7}
   call print_list(ref(list)) 
end subroutine

call main()
2
On

When passing an array of data you must include an empty set of brackets [] after the variable name. This was added to reduce the confusion between a regular variable and a variable containing an array of values.

So, in your case, change your method signature from subroutine print_list(list) to subroutine print_list(list[]).

Referring to this link in case you have any other questions.