Teach kids to code with BBC BASIC

In the 1980s, the UK was one of the world’s most computer-literate nations, and part of the reason was the BBC’s ambitious decision to invest in a computer to accompany a series of television programmes. That computer became known as the BBC Micro, and the language it helped teach millions of people was the “Beginners All-purpose Symbolic Instruction Code” (BASIC).

Found on most home computers of the time, BASIC had already been around for a while, and was designed to be easy to use. When the BBC commissioned its computer, it also commissioned an updated version that was designed to be as accessible as possible for beginners.

Today, there’s a version for Windows that brings the language into the 21st century, complete with the ability to access advanced Windows features. Alternatively, download a BBC Micro emulator.

While BASIC may not be as fashionable as it once was, its simplicity makes it a great place to start learning to code, and BBC BASIC in particular adds useful improvements found in more modern languages. There are also other versions of BASIC that can be used to write apps for Android, Windows, Linux, Mac and soon the iPhone – so picking up the essentials with BBC BASIC can provide a solid foundation.

Let’s look at a very simple BASIC program; this just asks your name, and when you type it in, says “hello” back:

10 INPUT “What is your name?” A$
20 PRINT “Hello” A$. “How are you?”

BBC emulator

Note that in older versions of BASIC, including the original BBC one, every line had a number; that isn’t required in newer versions.

Commands are always in upper-case; INPUT tells the computer to prompt the user for information, with the message in quotation marks, then stores it in the variable referred to as A$. The $ means it’s a sequence of letters rather than a number. The next line displays whatever was typed in, surrounded by “Hello” and “How are you?” Type this into the BBC Micro emulator, then type RUN, press return, and you have a simple program.

Repetition and decision making are the heart of many computer programs. Here’s an example of a BASIC request to print out times tables, which stops when the user enters zero:

REPEAT
INPUT “WHICH TIMES TABLE?” X%
FOR I% = 1 TO 12
PRINT I% ” TIMES ” X% ” = ” I%*X%
NEXT
UNTIL X% = 0

We’ve missed out the line numbers here; if you’re using a real BBC Micro, or BeebEm, enter “AUTO” first to have the line numbers added, and press Esc when you’re done.

Where our variables ended with a $ before, here they have a %; this means they’re whole numbers. Use neither and BBC BASIC will assume it’s a decimal number. REPEAT…UNTIL is one of the useful additions in BBC BASIC, but there are also other ways to make decisions. For instance, try adding this line after our INPUT (in BeebEm, add it as line 25):

IF X% = 0 THEN END

The program will stop before printing the 0 times table, which is neater. Or try changing it to the below and see what happens:

IF X% = 0 THEN END ELSE PRINT “WORKING…”

Two other useful tools in BBC BASIC are functions and procedures; these let you create a bit of code that you can use over and over again. Try this temperature-conversion code (type “NEW” to erase your old program, or just select File | New in BBC BASIC for Windows):

FOR I% = 0 TO 100 STEP 10
PRINT I% “C=” FN_CTOF(I%)
NEXT
END
DEF FN_CTOF(X)
=((X+40)*9/5)-40
END DEF

The DEF FN command lets us define our own function. We tell it that it will be given a value, “X”, and the line beginning “=” says what value we return, using brackets to make sure sums are done in the right order.

Make a temperature converter with graphics in BBC BASIC

Here, we look at a more complex example, using BBC BASIC for Windows – the code is below, or you can get the code here.

MODE 6
FOR I% = 400 TO 0 STEP -25
GCOL I%/25
CIRCLE FILL 640,500,I%
NEXT
END

There are special commands for drawing graphics, too; the set of circles pictured left is drawn with simple code – it’s pasted below, oryou can grab it here to see the proper indentation – where we must create the small circles last, on top of the big ones.

MODE 6
REPEAT
INPUT TAB(0,0) “Convert from C ” C
F = FN_CTOF(C)

CLG
GCOL 1
PROC_TEMPGRAPH(100,1)
GCOL 2
PROC_TEMPGRAPH(25,1)
GCOL 6
PROC_TEMPGRAPH(16,1)
GCOL 15
PROC_TEMPGRAPH(C,0)

PRINT TAB(0,23) F ” degrees Fahrenheit”
UNTIL C = 0
END

DEF FN_CTOF(X)
=((X+40)*9/5)-40
END DEF

DEF PROC_TEMPGRAPH(T,F)
R = T/100*400
IF F = 0 THEN
CIRCLE 640,500,R
ELSE
CIRCLE FILL 640,500,R
ENDIF
ENDPROC

You can even build apps that use a mouse and 3D graphics – things that weren’t around when the BBC Micro first appeared!

Step one: Get the temperature

In this code, we use a REPEAT…UNTIL loop to ask the user for a temperature, until they enter 0, and our function to convert it to Fahrenheit. By adding TAB (0,0) in the INPUT statement and (0,23) in the PRINT, we control where the text appears on screen. Now let’s add some graphics.

Step one

Step two: Put in the formula

Use DEF PROC to add a procedure; that is, code we’ll reuse. Our first parameter will be temperature, which we’ll use to calculate the radius. If the second is 0 we’ll just draw the outline of a circle. BBC BASIC for Windows lets you have an IF … THEN … ELSE that spans several lines, like this.

Step 2

Step three: Add colour

In our main code, we set the colour for graphics using the command GCOL; 1 is red, 2 is green, 6 is cyan. We call our TEMPGRAPH proc to draw circles for each, representing too hot, cosy and too cold. For example, PROC_TEMPGRAPH(25,1) here means it will fill the circles from 1 to 25 with red.

Step 3: add colour

Step four: Start the test

The final PROC_TEMPGRAPH(C,0) seen in step 3 adds a thin white line to show where our temperature sits in the circle. So, in the space of around 30 lines of code, we’ve created a temperature converter that also gives a graphical idea of how hot or cold each temperature is. Now tinker!

Step four

Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.