# Tips for Golfing in BBC BASIC

What general tips do you have for golfing in BBC BASIC? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to BBC BASIC (e.g. "remove comments" is not an answer).

• As a general comment, BBC Basic (and other old school Basics like C64 Basic and Spectrum Basic) are great for golfing graphics problems. There's no need to set up any parameters, because back when these machines were made, you had what the hardware gave you. On the other hand, they are also good general-purpose programming languages, not like some of the dedicated graphics languages (LOGO etc) which can be quite hard to do general purpose programming in. I will post some answers later. – Level River St Oct 19 '14 at 12:48

Spaces after keywords are optional because of tokenisation. For example:

INPUT answer$PRINT answer$
IF answer$= 'Hi' THEN PRINT 'Hi!' ENDIF  Can be shortened to INPUTanswer$
PRINTanswer$IFanswer$='Hi'THEN
PRINT'Hi!'
ENDIF


The corollary of this is, a variable name cannot start with a keyword. So PRINTa is not a valid variable name. However printa is.

Spaces before keywords can be more tricky. Variable names can end with a keyword, so if variable name and keyword are run together they will be interpreted as a variable name.

For example the following gives a "no such variable" error because the variable aPRINT is not defined:

  a=7
IF7=aPRINT"seven"


By rearranging this can be avoided:

  a=7
IFa=7PRINT"seven"


This is a trivial example, but frequently rearranging an expression to end in a number or bracket instead of a variable name will enable you to eliminate a trailing space.

• I'd love to see the parser code that allows this mess. – cat Apr 9 '16 at 17:06

Use VDU codes to perform graphics tasks.

All graphics in BBC basic was performed via machine-specific ASCII control characters. The VDU command exists to enable you to pass ASCII characters direct to the video controller.

For example in PLOT101,500,300 (draw a rectangle whose diagonal corners are the last position of the graphics cursor and the new point 500,300) can be subsituted by VDU25,101,500;300; The coordinates are followed by ; because they are double byte values. We can also replace 25,101, by 25881; which is the 16-bit little-endian representation of these 2 bytes.

Although this actually makes things longer when you have only one PLOT statement, if you have several it is shorter. When you have a long string of VDU codes every PLOT can be replaced by 25, (or if you declare a=25 simply by a,)

Here's an example of this technique: https://codegolf.stackexchange.com/a/26732/15599

Note that this is not just limited to PLOT. MODE, GCOL, COLOUR, ORIGIN etc can all work out shorter when expressed as a series of numeric values in a long VDU statement.

Very few of us have access to an original BBC micro, There's a whole range of interpreters at http://www.bbcbasic.co.uk/bbcbasic.html . Each implementation is slightly different.

My preferred interpreter is the first one on the list, "BBC Basic for Windows" written by RT Russell and available for Linux through Wine. It covers virtually all the features of original BBC Basic, including for example sound through the windows API, and has many enhancements. Its IDE and help are easy to use and feature packed.

It frequently allows you to omit colons between statements (which other implementations also do to some extent, but less) and it does not require you to number the lines of your program. On the downside, it only handles strings up to 255 characters and the evaluation version is restricted to 16k memory.

The other major family of BBC basics is Brandy Basic and its derivatives. Written in C, this is extremely portable, but has less features. For example it does not include sound, and rather than simply ignoring sound commands the program crashes. It is however able to handle longer strings than R T Russell's implementation so I have used it on occasion. There is no modern IDE, but it replicates BBC Basic's commandline fairly well (so it's not difficult to use if you are already familiar with BBC Basic.)

Use FN instead of PROC or GOSUB

GOSUB is very old fashioned. it requires a line number, so you have the following:

    GOSUB90
.
.
90 dosomething
RETURN


7 characters to call, 8 characters to define and return.

PROC doesnt require a line number. Here you have

    PROCa(x,y,z)
.
.
DEFPROCa(x,y,z)
dosomething
ENDPROC


5 characters to call (excluding arguments), 13 characters to define and return

With FN you have

    q=FNa(x,y,z)
.
.
DEFFNa(x,y,z)
dosomething
=1


5 characters to call (excluding arguments, but including the obligatory use of the return value.) 8 characters to define and return

FN wins pretty much all the time, especially if you can make use of the return value. If you have no arguments you can omit the brackets.

Use Abbreviated Keywords

Surprised this wasn't mentioned before, but BBC Basic supports abbreviated versions of keywords so that, eg:

1 FOR X=1 TO 10 STEP 2:PRINT X:NEXT X

Can be rewritten as (omitting space where possible too):

1F.X=1TO10S.2:P.X:N.X

Some keywords don't have abbreviations (eg, IF). The complete list is in the BBC Basic manual chapter 48, there's a handy copy here too.

The most surprising part is that abbreviations for some functions include the opening parenthesis, eg: PRINT MID\$("HELLO",3,2) is abbreviated as P.M."HELLO",3,2) - note the closing parenthesis is required even though the opening one was abbreviated away.

• Damn I did not know these existed – Beta Decay Feb 21 '20 at 18:48