R.T.Russell Home Page

Appendix B - Words We Mustn't Say




There are several common BASIC commands that are not mentioned in the main text, but you may have come across them elsewhere e.g. in the help files or other, lesser dialects of BASIC. You are advised not to use these in your programs as small children will mock you in the street as you walk past if you do. The words are GOSUB, ON .. GOTO, ON .. GOSUB and LET. Unlike GOTO, which still has its adherents, these keywords have definitely outlived their usefulness and BB4W offers much better alternatives. We will now make them stand at the front of the class so we can openly scorn them.

GOSUB

GOSUB is a distant relative of PROC. The idea was that you could split your program into sections and call them like a PROC. Unlike a PROC, you could only GOSUB to a line number rather than give a meaningful name. Once found, BASIC would jump off to the given line number, run the code until a RETURN statement was found (similar to an ENDPROC). Then it would jump back to the next statement after the GOSUB. Regrettably, you could not pass or return values and there was no provision for LOCAL or PRIVATE variables, everything had to be global. Awful - don't bother.

ON .. GOTO and ON .. GOSUB

Here a variable is used to decide which line number to jump to.

 10 REM Simple menu system
 20 CLS
 30 PRINT "Press 1 for option 1"
 40 PRINT "Press 2 for option 2"
 50 PRINT "Press 3 for option 3"
 60 INPUT "Enter choice: " C%
 70 ON C% GOTO 80,90,100
 80 PRINT "You chose option 1" : GOTO 110
 90 PRINT "You chose option 2" : GOTO 110
100 PRINT "You chose option 3" : GOTO 110
110 END
When X% is 1, the first line number (80) is selected, when X% is 2, the second (90) and so on. Again this very dependent on line numbers. ON .. GOSUB does the same job but uses subroutines as described above. In both options, use CASE, it's cleaner and easier to read and maintain.

ON .. PROC

BB4W has a variant to the above two called ON .. PROC which allows you to call a PROC dependent on the value of a variable. If the call to the procedures involves passing several arguments, the line quickly becomes unwieldy. The line could be split up using \ , but if you're spreading it over several lines, why not use CASE anyway?

LET

LET is used is to tell the computer that an assignment is about to take place. Using LET, our area calculator would become:

10 REM Area of a circle
20 LET Radius=1.5
30 LET Area=PI*Radius^2
40 PRINT "The area of the circle is ";Area
50 GOTO 20
60 END
Not that destructive in terms of structure, just a waste of bytes really. You don't need it.

Left CONTENTS

APPENDIX C Right


Best viewed with Any Browser Valid HTML 3.2!
© Peter Nairn 2006