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.
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?”
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:
INPUT “WHICH TIMES TABLE?” X%
FOR I% = 1 TO 12
PRINT I% ” TIMES ” X% ” = ” I%*X%
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%)
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.
FOR I% = 400 TO 0 STEP -25
CIRCLE FILL 640,500,I%
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.
INPUT TAB(0,0) “Convert from C ” C
F = FN_CTOF(C)
PRINT TAB(0,23) F ” degrees Fahrenheit”
UNTIL C = 0
R = T/100*400
IF F = 0 THEN
CIRCLE FILL 640,500,R
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 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 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 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!