0

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....

awyr_agored
  • 613
  • 3
  • 19
  • "Oh, dear ... is _this_ what `BASIC` has supposedly lately become?" I guess it's still the case that "you can sell any programming language, as long as you call it `BASIC`." ... _(Wink!)_ – Mike Robinson Jan 10 '19 at 15:42
  • Can I ask why this is tagged [bbc-micro]? – nekomatic Jan 21 '19 at 09:31

3 Answers3

2

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.

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
1

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()
awyr_agored
  • 613
  • 3
  • 19
0

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.

hmiedema9
  • 948
  • 4
  • 17
  • Thanks hmiedema9 - I did try that, but that also created a compile error caused by the parameter section. I appreciate your help though. Thank you – awyr_agored Jan 10 '19 at 16:09
  • Hmm, I don't have access to a computer I can work on compiling this myself right now, but I will try to get back to it when I can. Good luck – hmiedema9 Jan 10 '19 at 21:21