Contents / Structure within BASIC / Previous chapter / Next chapter / Index


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 quite straightforward - but in practice while the main aim is to write the letter there are often quite a few 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 those writing BASIC programs to break their programs up into recognizable 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

190 IF SCALE$="K" THEN GOSUB 430

200 IF NOT ( GOODSCALE AND TEMP>=-273.16) GOTO 260

210 PRINT''

220 PRINT TEMP; " Celsius"

230 PRINT TEMP+273.16; " Kelvin"

240 PRINT TENP*9/5 + 32; " Fahrenheit"

250 PRINT

260 IF GOODSCALE THEN 310

270 CLS

280 PRINT "You must follow the temperature with"

290 PRINT "the letter ""C"", ""F"" or ""K"" "

300 PRINT "and nothing else"

310 IF TEMP>=-273.16 THEN 360

320 CLS

330 PRINT "The temperature you have given is"

340 PRINT "too cold for this universe! Try again"

350 PRINT

360 GOTO 110

370 GOODSCALE=TRUE

380 GOTO 460

390 REM CONVERT TO CELSIUS

400 TEMP=(TEMP-32)*5/9

410 GOODSCALE=TRUE

420 GOTO460

430 REM CONVERT TO CELSIUS

440 TEMP=TEMP-273.16

450 GOODSCALE=TRUE

460 RETURN

Lines 430 to 460 are referred to as a "subroutine", and these lines of the program can be called from line 190 by the Statement GOSUB 430. Notice that this statement does not give the reader any idea of the purpose of the subroutine. The statement RETURN at the end of the subroutine returns it to the statement after the original GOSUB statement.

Compare the last program with the one that follows

10 REM TEMPERATURE CONVERSION

20 REM JOHN A COLL

30 REM VERSION 1.0 /22 NOV 81

40 MODE 7

50 @%=&2020A

60 PRINT "ENTER THE TEMPERATURE FOLLOWED BY"

70 PRINT "THE FIRST LETTER OF THE TEMPERATURE"

80 PRINT "SCALE. e.g. 100C or 72F or 320K"

90 REPEAT

100 PRINT

110 PRINT "Enter the temperature ";

120 INPUT REPLY$

130 TEMP = VAL(REPLY$)

140 SCALE$=RIGHT$(REPLY$,1)

150 GOODSCALE=FALSE

160 IF SCALE$="C" THEN PROCCENT

170 IF SCALE$="F" THEN PROCFAHR

180 IF SCALE$="K" THEN PROCKELVIN

190 PROCEND

200 UNTIL FALSE

210 END

220

230 DEF PROCCENT

240 GOODSCALE=TRUE

250 ENDPROC

260

270 DEF PROCFAHR

280 REM CONVERT TO CELSIUS

290 TEMP=(TEMP-32)*5/9

300 GOODSCALE=TRUE

310 ENDPROC

320

330 DEF PROCKELVIN

340 REM CONVERT TO CELSIUS

350 TEMP=TEMP-273.16

360 GOODSCALE=TRUE

370 ENDPROC

380

390 DEF PROCEND

400 IF GOODSCALE AND TEMP>=-273.16 THEN PROCRESULTS

410 IF NOT GOODSCALE THEN PROCILLEGAL

_SCALE

420 IF TEMP< -273.16 THEN PROCILLEGAL

_TEMP

430 ENDPROC

440

450 DEF PROCRESULTS

460 PRINT''

470 PRINT TEMP; " Celsius"

480 PRINT TEMP+273.16; " Kelvin"

490 PRINT TEMP*9/5 + 32; " Fahrenheit"

500 PRINT

510 ENDPROC

520

530 DEF PROCILLEGAL_SCALE

540 CLS

550 PRINT "You must follow the temperature with"

560 PRINT "the letter ""C"", ""F"" or ""K"" "

570 PRINT "and nothing else"

580 ENDPROC

590

600 DEF PROCILLEGAL_TEMP

610 CLS

620 PRINT "The temperature you have given is"

630 PRINT "too cold for this universe! Try again"

640 PRINT

650 ENDPROC

Certainly the second version is long (about a third longer) but it is much more understandable and this is of crucial importance for medium and large programs.

GOTO

You may have noticed the use of the GOTO statement in many of the examples above. GOTO is a very useful statement which tells the computer to skip to a particular line number. Beginners in programming find it easy to use. However, it should be used with care because it can lead to what some people call 'spaghetti' programming, a tangle of loops backwards and forwards which makes it very difficult indeed to follow what is going on. The example on page 32 shows this in an extreme form.

If you are writing short programs then by all means use GOTO.

For example, the following program prints out the ASCII code of any key which is pressed - useful if you can't find an ASCII code sheet:

10 PRINT GET

20 GOTO 10

It would be taking things too far to expect people to write

10 REPEAT

20 PRINT GET

30 UNTIL FALSE

However, when you write programs of more than, say, 50 lines it is a very good idea to try to use the "structure" provided instead of GOTO statements. It is generally accepted that it is still useful to use GOTO statements as a last resort when handling error conditions. Use whatever techniques make your program (a) work and (b) easy to follow.

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001