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.
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.
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
ON .. PROC
BBC BASIC 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:
Not that destructive in terms of structure, just a waste of bytes really. You don't need it.
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
CONTENTS |
APPENDIX C |