“MSX BASIC” COLUMN by Orazio Cacciola (part 11)
GOSUB/RETURN/ON GOSUB/ON GOTO/INKEY$
In this episode I will talk about the commands to call subroutines from any point of our program, how to use them when there are multiple choices and how to insert a character from the keyboard.
GOSUB
Send the program to a subroutine, at the end of which it will return to the next instruction. It is a command similar to GOTO, in both cases it sends execution to the line that follows immediately after the command. However, there is a difference between the two, while the first continues normally after making the "jump", the second memorizes the position where the jump occurred. This means that BASIC keeps in memory every GOSUB it encounters, in fact we can use all the GOSUBs we want, let's remember that BASIC's memory is not that much, the more we use it the more it will decrease.
SYNTAX: GOSUB Line Number
RETURN
Command used to close the routine called with GOSUB. Each GOSUB must have a RETURN. In fact, the RETURN also postpones the execution of the program
at the instruction following the GOSUB, frees memory. We can also decide not to return the program to the line following the GOSUB, by adding a line number.
SYNTAX: RETURN [Line Number]
ON GOSUB
It is used when there are more than one subprograms and we need to choose which one to execute. Let's say we want to create a program with 3 subroutines that will be executed according to the choice made: ON C GOSUB 100,200,300. The command must always have a variable, it is the one that decides where to send the execution. This last one will execute the 3 subroutines if it has the value between 1 and 3. We can say that the line numbers will be executed in an increasing way, therefore C = 1 will send the program to line 100, C = 2 to 200 and C = 3 to 300. The more subroutines there are, the more the variable used will increase. However, if the variable C is equal to 0, has a negative value or exceeds the number of command lines, in our case 4 or more, nothing happens. The command is skipped just like an IF/THEN function is when the condition is not met.
SYNTAX: ON Variable GOSUB Line number, [Line number], …….
ON GOTO
It has the same functionality as ON GOSUB and everything I said above applies, but as we know, GOTO creates the unconditional jump and nothing else. To be rerun you will have to use another GOTO.
INKEY$
Accepts any character that is pressed from the keyboard when the program encounters this function. If nothing is pressed at that time, returns an empty string. It can only contain one character. It is advisable to always use it with conditions (IF/THEN/ELSE) a good result when we have to make a choice and we don't want it to display anything on the screen. It can be used individually or within a variable. Below is a small example program.
100 A$=INKEY$: IF A$="" THEN 100 PRINT "CHOOSE YES/NO" 110 IF A$="S" OR A$="s" THEN 10 120 IF A$="N" OR A$="n" THEN CLS: END ELSE 100
As I did last time, I created a small program to help you understand better.
10 DEFINT AZ: SCREEN 0 20 CLS:LOCATE 15,1: PRINT "MENU": GOSUB 100 30 S$=INKEY$: IF S$="" THEN 30 40 IF S$<"1" OR S$>"4" THEN 30 50 ON VAL(S$) GOSUB 200,300,400,500 60 GOTO 30 100 LOCATE 1,4 : PRINT"1 - ENTRY DATA" 110 LOCATE 1,8 : PRINT"2 - MODIFY DATA" 120 LOCATE 1,12: PRINT"3 - VIEW DATA" 130 LOCATE 1,20: PRINT"4 - PROGRAM EXIT" 140 RETURN 200 CLS: LOCATE 12,0: PRINT "INSERT" 210 LOCATE 1,4:INPUT "NAME";N$ 220 LOCATE 1,8: INPUT "ADDRESS";I$ 230 LOCATE 1,12:INPUT "AMOUNT";IM 240 LOCATE 1,18: PRINT "CORRECT (Y/N)?" 250 S$=INKEY$: IF S$="" THEN 250 260 IF S$="s" OR S$="S" THEN RETURN 20 270 IF S$="N" OR S$="N" THEN 200 ELSE 250 300 CLS:LOCATE 15 ,0: PRINT "EDIT" 310 LOCATE 0,4:INPUT "NAME(Confirm not to change)";N1$ 320 IF N1$="" THEN LOCATE 1,29:PRINT N$ ELSE N$=N1$ 330 LOCATE 0,8: INPUT "ADDRESS(Confirm not to change)";I1$ 340 IF I1$="" THEN LOCATE 0,9:PRINT I$ ELSE I$=I1$ 350 LOCATE 0,12:INPUT "AMOUNT(Confirm not to change)";IM$ 360 IF IM$ ="" THEN LOCATE 0,13:PRINT IM ELSE IM=VAL(IM$) 370 CLS:RETURN 400 LOCATE 10,0: PRINT "DISPLAY" 410 LOCATE 1,4: PRINT "NAME :";N$ 420 LOCATE 1,8 : PRINT "ADDRESS :";I$ 430 LOCATE 1,12: PRINT "AMOUNT :";IM 440 LOCATE 1,18:PRINT "Press space to exit" 450 IF INKEY$=" " THEN RETURN ELSE 450 500 CLS:END
I didn't use ON GOTO, but just change line 50 and the various subroutine lines by replacing RETURN with GOTO Line number and you're done. The program was made simply due to space constraints, but you can make it much more “presentable” by making your own changes.
Until next time, thanks everyone.



0 Comments