Error when using LOCAL in procedure

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
User avatar
zachnoland
Posts: 24
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Error when using LOCAL in procedure

Post by zachnoland »

I am implementing a simple algorithm using bbcsdl. The algorithm that I am implementing is SWAP or exchanging numbers or data in an array. I started to create my own procedures to make it easier to use BASIC code. :D

But I get an error, I don't know what this error is because I think it shouldn't cause an error. :|
Then I read the reference for the error message I read it but it was quite strange, I don't think I did anything wrong https://www.bbcbasic.co.uk/bbcwin/manual/bbcwinc.html
:geek:

Here is my code:

Code: Select all

   10 REM EXAMPLE ALGORITHM SWAP VALUE IN ARRAY
   20
   30 DIM NUM%(5)
   40
   50 PROCINP_ARR(NUM%())
   60
   70 PRINT "BEFORE SWAP ARRAY"
   70 PROCPRALL(NUM%())
   80
   90 PRINT "AFTER SWAP ARRAY"
  100 PROCSW_ELEM(NUM%(), 0, 4)
  110 PROCPRALL(NUM%())
10000
10001 END
10002
10003 DEF PROCINP_ARR(ARR%())
10004 LOCAL DAT%
10005 FOR I% = 0 TO DIM(ARR%(), DIM(ARR%())) - 1
10006   PRINT "INPUT ELEMENT "; I% + 1;
10007   INPUT DAT%
10008   ARR%(I%) = DAT%
10009 NEXT I%
10010 ENDPROC
10011
10012 DEF PROCPRALL(ARR%())
10013 FOR I% = 0 TO DIM(ARR%(), DIM(ARR%()))
10014   PRINT ARR%(I%); " ";
10015 NEXT I%
10016 PRINT " "
10017 ENDPROC
10018
10019 DEF PROCSW_ELEM(RETURN ARR%(), IDX1%, IDX2%)
10020 LOCAL HND% = ARR%(IDX1%)
10021 ARR%(IDX1%) = ARR%(IDX2%)
10022 ARR%(IDX2%) = HND%
10023 ENDPROC
Error is said
Not in a function at line 10020
Richard Russell
Posts: 540
Joined: Tue 18 Jun 2024, 09:32

Re: Error when using LOCAL in procedure

Post by Richard Russell »

zachnoland wrote: Wed 10 Dec 2025, 04:40 But I get an error, I don't know what this error is because I think it shouldn't cause an error. :|
You have a Syntax Error in line 10020:

Code: Select all

10020 LOCAL HND% = ARR%(IDX1%)
This code is invalid, but rather than reporting an error BBC BASIC is making as much sense of it as it can and is interpreting it as:

Code: Select all

      LOCAL HND%
      = ARR%(IDX1%)
The first statement makes HND% local and the second is a return from function statement - but since it is not inside a function it reports the 'Not in a function' error that you are experiencing.

Presumably what you meant to write is:

Code: Select all

      LOCAL HND%
      HND% = ARR%(IDX1%)
      ARR%(IDX1%) = ARR%(IDX2%)
      ARR%(IDX2%) = HND%
but since BBC BASIC has a built-in SWAP statement it would be easier and faster to write:

Code: Select all

      SWAP ARR%(IDX1%), ARR%(IDX2%)
User avatar
zachnoland
Posts: 24
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Re: Error when using LOCAL in procedure

Post by zachnoland »

Richard Russell wrote: Wed 10 Dec 2025, 09:29

Code: Select all

      LOCAL HND%
      HND% = ARR%(IDX1%)
      ARR%(IDX1%) = ARR%(IDX2%)
      ARR%(IDX2%) = HND%
Thanks, I thought direct initialization on the LOCAL statement would work, but it doesn't. So it has to be declared as LOCAL first. :D
Richard Russell wrote: Wed 10 Dec 2025, 09:29 but since BBC BASIC has a built-in SWAP statement it would be easier and faster to write:

Code: Select all

      SWAP ARR%(IDX1%), ARR%(IDX2%)
Wow cool, faster. I am implementing the basic algorithm, imitating the SWAP statement how it works. 8-)
Richard Russell
Posts: 540
Joined: Tue 18 Jun 2024, 09:32

Re: Error when using LOCAL in procedure

Post by Richard Russell »

zachnoland wrote: Wed 10 Dec 2025, 09:59 Thanks, I thought direct initialization on the LOCAL statement would work
That's a reasonable expectation, because in C for example you can initialise a variable in a declaration. But not in BASIC, usually (there is an implied initialisation to zero in this case).

For those interested in BBC BASIC history, there's a difference in the way Acorn's versions of BBC BASIC and mine behave with your code. In Acorn's versions line 10020 would indeed have reported a 'Syntax error' whereas mine parse it as two separate, valid, statements. :o

To report an error Acorn's versions basically have to test for two different delimiters: comma (meaning another variable follows in the LOCAL list) and colon - or another statement separator like ELSE - indicating the end of the LOCAL statement. If they see neither a comma or a statement delimiter they report a 'Syntax error'.

But testing for the additional delimiters takes time. In my BASICs I test only for a comma; if present it indicates that another variable follows in the LOCAL list and if not it assumes that the statement has ended. Hence the subsequent code is treated as a new statement. This was a deliberate design decision on my part to sacrifice clarity in error reporting for speed.

Generally my interpreters do not include code for the sole purpose of reporting a Syntax Error, they report an error only when the interpreter cannot make sense of the code and therefore cannot continue execution. This improves the overall speed a little, at the cost of occasionally less clear error reporting.