Contents / Structure within BASIC / Previous chapter / Next chapter / Index
The BBC computer has a very complete version of BASIC - often called "Extended BASIC" - and in addition it includes the ability to define and use procedures and functions. It is probably the first version of BASIC in the world to allow full procedure and function handling. These extremely powerful features enable the user to structure his or her programs easily and in addition provide a real introduction to other computer languages like PASCAL.
A procedure is a group of waste statements which can be "called by name" from any part of a program.
10 REM REACT
20 REM JOHN A COLL
30 REM BASED ON AN IDEA BY THEO BARRY, OUNDLE
40 REM VERSION 1 / 16 NOV 81
50 @%=&2020A
60 ON ERROR GOTO 470
70 MODE7
80
90 PROCINTRO
100 REPEAT
110 PROCFIRE
120 PROCSCORE
130 UNTIL FNSTOP
140 END
150
160 DEF PROCINTRO
170 PRINT "This program tests your reactions"
180 PRINT
190 PRINT "Press the space bar to continue"
200 REPEAT UNTIL GET=32
210 CLS
220 ENDPROC
230
240 DEF PROCFIRE
250 CLS
260 PRINT "Press the space bar"
270 PRINT "as soon as a cross appears"
280 T=TIME
290 R=RND(200)+100
300 REPEAT UNTIL TIME>T+R
310 PRINT TAB(17, 10);"+"
320 *FX 15,1
330 REPEAT UNTIL GET=32
340 DELAY=TIME-T-R
350 ENDPROC
360
370 DEF PROCSCORE
380 PRINT TAB(0, 22);
390 PRINT "You took "; DELAY/100;"
seconds"
400 ENDPROC
410
420 DEF FNSTOP
430 PRINT"Do you want another go?"
440 REPLY$=GET$
450 = (REPLY$="N") OR (REPLY$="n")
460
470 @%=10
The program above shows how named procedures and functions can be used. The main program is between line 90 and line 140
90 PROCINTRO
100 REPEAT
110 PROCFIRE
120 PROCSCORE
130 UNTIL FNSTOP
140 END
The program tests a person's reactions by measuring how long it takes him to notice a cross on the screen. As you will see from the section above line 90 calls a procedure which gives an introduction. The procedure is called PROCINTRO and it produces the following on the screen.
This program tests your reactions
Press the space bar to continue
Then the program repeats PROCFIRE and PROCSCORE until the user indicates that he or she does not wish to continue.
PROCFIRE produces this
Press the space bar
as soon as a cross appears
+
and PROCSCORE produces this
You took 2.03 seconds
It all seems very straightforward and logical - and so it is. Using procedures enables you to split a problem up into a number of small manageable sections and to use (or call) those sections with a sensible name. The main section of most programs should be just a number of procedure calls as are lines 90 to 140. The procedures themselves should be in a separate section - after the END statement.
Let us examine PROCINTRO more closely
160 DEF PROCINTRO
170 PRINT "This program tests your reactions"
180 PRINT
190 PRINT "Press the space bar to continue"
200 REPEAT UNTIL GET=32
210 CLS
220 ENDPROC
Notice how it is defined: line 160 is the start of the definition and the procedure ends at line 220 between those lines are normal BASIC statements. Lines 170, 180 and 190 just print messages on the screen. Line 200 waits until the space-bar is pressed, after which line 210 clears the screen.
There are a number of more complex things that can be done with procedures and another program will illustrate the use of parameters - variables passed to the procedure from the main program.
10 REM HYPNO
20 REM TIM DOBSON / ACORN COMPUTERS
30 REM VERSION 2 / 16 NOV 81
40 MODE 5
50 VDU29,640;512;
60
70 FOR X=510 to 4 STEP -7
80 GCOL0,X
90 PROCBOX(X)
100 NEXT
110
120 REPEAT
130 GCOL RND(4),RND(4)
140 FOR K=0 TO 500 STEP 8
150 PROCBOX(K)
160 NEXT K
170 GCOL RND(4),RND(4)
180 FOR K=500 TO 0 STEP -9
190 PROCBOX(K)
200 NEXT K
210 UNTIL FALSE
220 END
230
240 DEF PROCBOX(J)
250 MOVE -J,-J
260 DRAW -J,J
270 DRAW J,J
280 DRAW J,-J
290 DRAW -J,-J
300 ENDPROC
The program uses a procedure called PROCBOX which draws a box. The size of the box is determined by the parameter J in the procedure. However you will see that in line 90 the procedure is called with the statement PROCBOX(X). The initial value of X will be 510 because that is the starting value of the FOR loop at line 70. This value of X (510) will be passed to the parameter J in the procedure. As a result the procedure PROCBOX will draw a box of "size" 510. J is called the "formal parameter" for the procedure since it is used in the procedure itself. However the X in line 90 and the K in line 150 are referred to as actual parameters. Whatever value K has in line 150 will be transferred to the formal parameter J.
A procedure may have any number of parameters but there must be exactly the same number of actual parameters when the procedure is called as there are formal parameters in the procedure definition. Thus if a procedure was defined like this
1000 DEF PROCSWITCH(A,B,C$)
1040 ENDPROC
it could not be called with a statement like
150 PROCSWITCH(X,Y)
but this would be acceptable
150 PROCSWITCH(length, height, NAME$)
10 J=25
20 FOR X=1 TO 5
30 PROCNUM(X)
40 PRINT "OUT OF PROCEDURE J= ";J
50 NEXT X
60 END
70 DEF PROCNUM(J)
80 PRINT "IN PROCEDURE J= ";J
90 ENDPROC
>RUN
IN PROCEDURE J= 1
OUT OF PROCEDURE J= 25
IN PROCEDURE J= 2
OUT OF PROCEDURE J= 25
IN PROCEDURE J= 3
OUT OF PROCEDURE J= 25
IN PROCEDURE J= 4
OUT OF PROCEDURE J= 25 IN PROCEDURE J= 5
OUT OF PROCEDURE J= 25
In the program above the variable J is used in two ways. The main program starts at line 10 and ends at line 60. The procedure is defined between lines 70 and 90. Line 10 declares that J has the value 25 and the value of J is not changed in the main program. However J is used as the formal parameter in the procedure. All formal parameters are LOCAL to the procedure which means that their value is not known to the rest at the program. Inside the procedure, J takes on the value of the actual parameter X, but outside the procedure it has a different value. The distinction is made between GLOBAL variables and LOCAL variables. Global variables are known to the whole program, including procedures, whereas local variables are only known to those procedures in which they are defined and to procedures within that procedure.
In the program above, X is a global variable and it looks as if J is global too, since it is defined in line 10 of the main program. In fact that J is global but the use of the parameter J in the procedure creates another variable J which is local to the procedure. If a different parameter had been used in the procedure definition then J would have remained global. Thus in the print-out below the formal parameter has been changed to K in line 70, which leaves J as a global variable.
10 J=25
20 FOR X=1 TO 5
30 PROCNUM(X)
40 PRINT "OUT OF PROCEDURE J= ";J
50 NEXT X
60 END
70 DEF PROCNUM(K)
80 PRINT "IN PROCEDURE J= ";J
90 ENDPROC
>RUN
IN PROCEDURE J= 25
OUT OF PROCEDURE J= 25
IN PROCEDURE J= 25
OUT OF PROCEDURE J= 25
IN PROCEDURE J= 25
OUT OF PROCEDURE J= 25
IN PROCEDURE J= 25
OUT OF PROCEDURE J= 25
IN PROCEDURE J= 25
OUT OF PROCEDURE J= 25
The program is pretty pointless in its present form for several reasons - not least because it doesn't actually do anything with K in the procedure!
Now that J is global its value could be altered anywhere - including inside the procedure. Line 75 increases J by 10:
10 J=25
20 FOR X=1 TO 5
30 PROCNUM(X)
40 PRINT "OUT OF PROCEDURE J= ";J
50 NEXT X
60 END
70 DEF PROCNUM(K)
75 J=J+10
80 PRINT "IN PROCEDURE J= ";J
90 ENDPROC
>RUN
IN PROCEDURE J= 35
OUT OF PROCEDURE J= 35
IN PROCEDURE J= 45
OUT OF PROCEDURE J= 45
IN PROCEDURE J= 55
OUT OF PROCEDURE J= 55
IN PROCEDURE J= 65
OUT OF PROCEDURE J= 65
IN PROCEDURE J= 75
OUT OF PROCEDURE J= 75
It has been pointed out that all formal parameters are local to the procedure in which they are defined (and to inner procedures) but other variables can be declared as LOCAL if required. We very often use the variable X as a counter for a FOR...NEXT loop and as a result you have to be careful not to use it twice in the same section of a program. Declaring X as local to a procedure ensures that its use locally will not affect the value of X outside the procedure.
10 J=25
20 FOR X=1 TO 5
30 PROCNUM(X)
40 PRINT "OUT OF PROCEDURE J= ";J
50 NEXT X
60 END
70 DEF PROCNUM(K)
72 LOCAL X
75 FOR X= 1 TO 10
80 J=J + J/X
85 NEXT X
90 ENDPROC
>RUN
OUT OF PROCEDURE J= 275
OUT OF PROCEDURE J= 3025
OUT OF PROCEDURE J= 33275
OUT OF PROCEDURE J= 366025
OUT OF PROCEDURE J= 4026275
In the program above, X is used twice - once in the main program (lines 20 to 50) and secondly, and quite separately, as a LOCAL variable in the procedure. J remains global.
It is wise to declare variables as LOCAL in procedures and functions wherever possible (except when the variable is a formal parameter).