Acorn Computers BBC Micro Model B w/8271 — Acorn BBC Micro Model B w/8271 User Guide Manual PDF

96 19 GOSUB This statement allows the program temporarily to divert to another section. Think about the process of writing a letter. In essence it is really a straightforward procedure – but in practice while the main aim is to write the letter there are often several diversions like the need to get another sheet of paper or answer the phone. These small ‘sub-tasks’ are essential but if we write a description of every single thing that occurred while writing a letter the reader would probably be so confused that he or she wouldn’t realise what the overall aim was. However if the job is described as a series of subroutines or procedures then the main task will emerge more clearly.

The subroutine and the GOSUB statement were introduced some years ago to help people who write BASIC programs to break their programs up into recognisable modules. In recent years more flexible and more easily used tools have become available – namely procedures and functions – and these two should be used in preference to GOSUB . None the less, BBC BASIC maintains the GOSUB statement for compatibility with other versions of BASIC. A temperature scale conversion program is shown in two forms below. Both produce exactly the same output on the computer screen but one has been written using GOSUB and GOTO and the other using procedures. First with GOSUB and GOTO :

10 REM TEMPERATURE CONVERSION 20 REM WITHOUT STRUCTURED BASIC 30 REM THIS IS NOT THE WAY TO WRITE PROGRAMS! 40 REM JOHN A COLL 50 REM VERSION 1.0 /22 NOV 81 60 MODE 7 70 @%=&2020A 80 PRINT "ENTER THE TEMPERATURE FOLLOWED BY" 90 PRINT "THE FIRST LETTER OF THE TEMPERATURE" 100 PRINT "SCALE. e.g. 100C or 72F or 320K" 110 PRINT 120 PRINT "Enter the temperature "; 130 INPUT REPLY$ 140 TEMP = VAL(REPLY$) 150 SCALE$=RIGHT$(REPLY$,1) 160 GOODSCALE=FALSE 170 IF SCALE$="C" THEN GOSUB 370 180 IF SCALE$="F" THEN GOSUB 390