Problem with GOTO command

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
circe
Posts: 12
Joined: Wed 07 May 2025, 09:26

Problem with GOTO command

Post by circe »

If a Program contains a GOTO instruction it executes correctly, BUT if the GOTO is contained within a file loaded by:
INSTALL "path\filename.BBC", GOTO tries to pass control to the controlling program, not to the segment issuing the GOTO command, and produce unexpected results :o

I know that GOTO is depreciated, but have reasons for wanting to use it.

We seem to have lost the ability to drop .BBC files onto the Forum, so sample attached in a ZIP

IF you unzip to a file TEST-GOTO.BBC and do:
INSTALL "path\TEST-GOTO.BBC"
PROCi_recs
PROCj_recs
END
Having adjusted the path, this should demonstrate the problem, running TEST-GOTO.BBC on its own is OK.
You do not have the required permissions to view the files attached to this post.
Richard Russell
Posts: 538
Joined: Tue 18 Jun 2024, 09:32

Re: Problem with GOTO command

Post by Richard Russell »

circe wrote: Tue 09 Dec 2025, 17:16 I know that GOTO is depreciated, but have reasons for wanting to use it.
Although you can use GOTO in your main program, if you must, it simply won't work within a sub-module or library because the destination of a GOTO is found by scanning the program - linearly from the beginning - until the specified line number is found, and if that line isn't in the main program it never will be!

It's worth noting that because of this crude method of finding the destination line, GOTO and GOSUB are very slow, particularly if the program is large. Including GOTO and GOSUB in the language was essential for compatibility with other BASICs, but making them slow added another incentive not to use them. :D
Having adjusted the path, this should demonstrate the problem, running TEST-GOTO.BBC on its own is OK.
So, you want to 'fall through' from one procedure to the next (something which itself is not to be encouraged) without resetting the DATA pointer? The easiest way to achieve that is to place the RESTORE statement on the DEF PROC line, thus:

Code: Select all

 9270 DEFPROCi_rec_data
 9280 REM:Valid TLCs in I-records
 9290 RESTORE  +1
 9300 DATA ONE,TWO,THREE
 9310 REM Falls through to PROCj_rec_data...
 9320
 9330 REM:*************************
 9340 REM:* Check for J-records.  *
 9350 REM:*************************
 9360
 9370 DEFPROCj_rec_data : RESTORE +1
 9380 REM:Valid TLCs in J-records
 9390
 9410 DATA EIGHT,NINE,TEN
 9420 REM:More processing Here
 9430 DATA ****
 9440 ENDPROC
 9450 REM:***********************
Anything placed on the DEF PROC line will be executed only when that procedure is called, not when execution 'falls through' from above.
Richard Russell
Posts: 538
Joined: Tue 18 Jun 2024, 09:32

Re: Problem with GOTO command

Post by Richard Russell »

Richard Russell wrote: Tue 09 Dec 2025, 18:03 Anything placed on the DEF PROC line will be executed only when that procedure is called, not when execution 'falls through' from above.
This is a commonly-used technique to simulate, in a way, a polymorphic function - i.e. a function which can take different numbers or types of parameters.

Suppose one has a library function that exists in four forms: a base function that takes a set of parameters, a variant of that function which takes an additional parameter type%, a second variant which takes an additional parameter flag% and a third variant which takes both those additional parameters. When the additional parameters are not supplied they default to zero.

Here's how that can be implemented:

Code: Select all

      DEF PROC_polymorphic(parameters) : LOCAL type%, flag%  
      DEF PROC_polymorphic_1(parameters, type%) : LOCAL flag%  
      DEF PROC_polymorphic_2(parameters, flag%) : LOCAL type%  
      DEF PROC_polymorphic_3(parameters, type%, flag%)  
      REM Common code here....  
      ENDPROC
Of course if you want the omitted parameter(s) to default to something other than zero, you can add appropriate initialisation - also on the DEF PROC line(s):

Code: Select all

      DEF PROC_polymorphic(parameters) : LOCAL type%, flag% : type% = 1 : flag% = 2  
      DEF PROC_polymorphic_1(parameters, type%) : LOCAL flag% : flag% = 2  
      DEF PROC_polymorphic_2(parameters, flag%) : LOCAL type% : type% = 1  
      DEF PROC_polymorphic_3(parameters, type%, flag%)  
      REM Common code here....  
      ENDPROC
Unlike 'true' polymorphic functions you must explicitly call the variant you need, rather than the language working it out from the number and types of parameters supplied, but it's as close as you can get in BBC BASIC.