50 PRINT"x_position% is " ; x_position%
60 PR0Ccentre("A few characters")
70 PRINT" length% i s now " ; length%
80 PRINT"x_position% is now " ; x_position%
90 END
10 DEFPROCcentre(text$)
110 length%=LEN(text$)
120 x_posi ion%=(40_length%) /2
130 PRINT TAB(x_position%) text$
140 ENDPROC
You can make sure that variables within a procedure do not interfere with the
rest of the program by declaring the variables as local. Add this line to the
previous program and run it again:
105 LOCAL length%,x_position%
This time length% and x_position% are unchanged despite PROCcentre.
There are effectively two copies of the variables: the global values, available to
the whole program, and the local values, which exist only within PROCcentre.
PROCcentre is now completely isolated, and it can be used in any program
without giving unexpected side-effects.
Note that variables can also be used as parameters. This brief program
contains an improvement on PROCcircle so that you can select the colour used:
10MODE 135
20 PR0Cchoose
30 MODE 130
40 PR0Ccircle(xchoice%,ychoice%,radius_choice%,colour_choice%)
50 END
60 DEFPROCchoose
70 INPUT"Centre of circle " ,xchoice%,ychoice%
80 INPUT"Radius " ,radius_choice%
90 INPUT"Colour number ( 1 to 15) " ,colour_choice%
100 ENDPROC
110 DEFPR0Ccircle(xcentre% ,ycentre%,radius%,colour%)
120 GCOL 0,colour%
130 MOVE xcentre%,ycentre%
140 PLOT 157,xcentre%+radius%,ycentre%
150 ENDPROC
Throughout the rest of this chapter on BBC BASIC, procedures are used
extensively. This is because it is simpler to write and modify programs that are
broken into smaller sections. Some of the procedures will be specific to a
C 35