SECTION H JUMPS AND SUBROUTINES FILE INTER.H A 'jump' in a program is simply where the program jumps over lines to a different part of the program. In other words, where lines are not executed in numerical order. Sometimes conditions have to be met before the jump is allowed, and this is called a 'conditional jump'. Where the jump is a direct instruction with no conditions laid down it is called an 'unconditional jump' GOTO An unconditional jump to a line number.This ===== is a direct instruction to jump to a given line . e.g GOTO 500 means jump to line number 500. It is looked down on by many pundits as producing a disorganised program, as it certainly can if used indiscriminately, but it is a very common and much used command. GOSUB ...... RETURN An unconditional jump to a subroutine. A ===== subroutine is a short piece of program which does a specific job and ends with the command RETURN. The command GOSUB 20000 would cause the program to jump to line 20000 and continue from there until it met the RETURN at the end of the subroutine, when it would return to the place where the jump originated and continue to execute the program from the instruction immediately following the GOSUB command. Look at the following piece from a program. 60 GOSUB 680 : PRINT cls$ ........... ........... 680 PRINT " Press [RETURN] to continue " 690 WHILE INKEY$<>CHR$(13):WEND 700 RETURN When the program reaches line 60, the GOSUB tells it to jump to line 680 and look out for a RETURN. It will execute the instruction on line 680, then the one on line 690 until, when it reaches line 700 it finds the RETURN it has been expecting. It will then return control back to line 60, execute the command PRINT cls$, and carry on with the program. In the example above, unless precautions are taken, the program could continue until it got down to line 680 again, which it would naturally execute again,then 690 and reach the RETURN on line 700. The program would stop and the error message 'Unexpected RETURN' would appear on screen, because a RETURN has been found without the corresponding GOSUB. To guard against this, subroutines are placed where they cannot be accidentally executed, often at the end of a program, after an END command. If you need to use the same piece of programming several times in a program always use a subroutine with the GOSUB ..... RETURN commands. It not only saves typing and memory, but makes for less errors and easier modifications to programs. Whenever similar tasks are needed several times during a program, try to use a subroutine. It may be that part of the task has to be retained in the main body of the program, whilst part can be made into a subroutine. For example, if your program is in several parts, with each part requiring a highlighted heading at the top of the screen, you could store each heading in a variable, title$, in the body of the program, then GOSUB to a subroutine which included PRINT title$ in it. Thus all the spacings, reverse video effects etc. would be taken care of by the subroutine . 4000 title$= " Selling price for Widgets": GOSUB 18000 :::::::::::::: 5000 title$= " Stock levels - Widgets ": GOSUB 18000 :::::::::::::: 6000 title$= " Bought-in materials - Widgets":GOSUB 18000 ::::::::::::: ::::::::::::: ::::::::::::: 17990 END 18000 PRINT cls$: 18010 PRINT FN posn$(5,30) "title$" 18020 RETURN At each GOSUB call to the subroutine the screen would clear and the appropriate title would be printed on the 5th row, starting at column 30. Whilst I have shown a simple subroutine here, hardly worth bothering with, it should be easy to see the possibilities. It is quite in order to have a GOSUB command from within a subroutine provided the RETURNS are correct, and this is common practice. CONDITIONAL JUMPS ***************** IF.....THEN.....ELSE This command can be written in several ways =================== but whichever form you favour, it's use is the same. The first two terms are often used alone. 'IF n<3 THEN 1230 'means 'IF n is less than 3 THEN GOTO line 1230' We can see that in this case THEN is short for THEN GOTO, but you could have put just GOTO i.e IF n<3 GOTO 1230. The third term ELSE brings in an alternative :- ' IF n<0 THEN 1230 ELSE 1500 ' where ELSE means ELSE GOTO. The command can be nested to include more conditions :- 50 IF n<0 OR n>6 THEN 1230 ELSE 1500. or:- 50 IF n>1 AND n<10 THEN 1230 ELSE PRINT " A number between 1 and 10, please In the last example, the operator has been asked previously to enter a number between 1 and 10. If this is done correctly, the program continues with a jump to line 1230 else the error message is printed. The IF command can be used for other purposes where conditions have to be met :- 450 IF (condition) THEN PRINT " Text 1 " ELSE PRINT "Text 2 " Translating; if the condition is true print "text 1", if not true print "Text 2". ON......GOTO Conditional jump to a line number ============ ON......GOSUB Conditional jump to a subroutine. ============= Both of the above commands work in the same way and a typical line is:- 2010 ON number GOTO 3000,4000,5000,6000 Where number is a numeric variable ( represents a number). Suppose for example that the program asks for an input of a number between 1 and 4 and the number is stored in variable 'number' If 'number represents 1, the jump would be to line 3000, if 2; line 4000 etc. ON ERROR GOTO Conditional jump to line number. If an error is ============= found in the running of a program, BASIC has a nasty habit of going immediately into direct mode, which could mean at least inconvenience if not serious loss of entered data, or in file handling, spoiled files. It is outside the scope of this course to go into this in detail, but I will present one interesting possibility. Suppose that you have written a program which calls up another program by one of the several commands; TYPE, CHAIN etc. You run the program, but it cannot find the asked-for file. It will immediately end and print an error message " File not found ". To avoid this you put near the start of the program :- "ON ERROR GOTO 12000 " where 12000 represents a convenient line number . On line 12000 onward you put:- 12000 PRINT " File is on other side of disc. Turn disc over and press [RETURN]" 12010 WHILE INKEY$<>(CHR$(13):WEND 12020 RESUME 4000 The effect is this; the program has been primed to call line 12000 in the event of a problem, line 12000 prints the message re. the disc location, while line 12010 halts the program awaiting a key press .The disc is turned over, [RETURN] is pressed and line 12020 returns control to line 4000,(unconditional jump) where the problem originated. Thus the program continues smoothly. As in most examples, this is one choice from many possible alternatives. GENERAL NOTES ************* If you write a program which starts at the first line and executes each line in numerical order it will be very easy to understand, but it is seldom that such a program can be written. Most program have some jumps, some have many. It is good practice to:- 1) Use GOSUB in preference to GOTO where practicable. 2) Keep your subroutines together, preferably at the end of the program and separated from the main program lines by the END command. 3) Label your subroutines with a REM note. 4) Label your GOSUB and important GOTO commands with a REM note. Otherwise it is easy to lose your way. Remember that unwise use of GOTO can set up accidental loops. End of File INTER.H accidental loops.