English
Language : 

NSB8 Datasheet, PDF (106/158 Pages) List of Unclassifed Manufacturers – BASIC interpreter for Z80 family
grifo®
ITALIAN TECHNOLOGY
SUBROUTINES
When writing programs, you will often find that you need to repeat what amounts to essentially the
same sequence of statements at various separate locations in the program text. For example, your
program may require the user to answer yes or no to certain questions. After writing the program,
you find that sequences similar to that below occur several times in the text:
10 REM Get yes (Y) or no (N) answer in A$.
20 INPUT “PLEASE ANSWER YES OR NO: “,A$
30 IF A$=” “ THEN 20 \ REM No answer given.
40 A$=A$ (1,1)
50 IF A$=”Y” THEN 70 \ REM OK answer
60 IF A$<>”N” THEN 20 \ REM Not = Y or N either.
70 REM At this point, answer was Y or N.
It is certainly troublesome for you (and a waste of program space besides) to type the same sequence
of statements over and over again. If you required several such answers at one point in the program,
of course, you could use a loop to repeat the statements as often as necessary. However, the problem
is different when you must perform the same actions in different parts of the program.
A very nice solution to this problem involves writing just one copy of the segment at one point in the
program, then somehow telling BASIC to rexecute that part whenever necessary. That is, at those
points in the program where you need to get a yes or no answer, BASIC would jump over to the part
of the program which gets the answer, then return to the original point to continue on with whatever
should happen after the answer has been obtained.
In this situation, the answer segment would be called a SUBROUTINE. This subroutine would be
invoked (or called) from other parts of the program to perform its single, important task.
NSB8 makes available two special statements which provide subroutine capability (both are
described in detail in their own sections). The first is GOSUB, which is used to call a subroutine. The
GOSUB keyword is followed by a line number, which tells BASIC where the subroutine begins in
the program text. BASIC reacts to a GOSUB by transferring execution to the specified line number,
while remembering the point where the subroutine was called. The action of the GOTO is similar,
but no calling location is remembered, which makes GOTO unsuitable for subroutine calling. When
the subroutine is finished, BASIC uses the remembered location to return to the point in the program
immediately after the subroutine was called. BASIC knows when a subroutine is finished only when
it executes a RETURN statement. RETURN merely says to BASIC: go back to the calling point
now. It is not necessary to make RETURN the last physical statement in a subroutine, though it turns
out that, in practice, this usually happens.
The answer program segment above may be turned into a legal BASIC subroutine merely by
replacing the last REM statement with RETURN, and translating the appropriate line numbers:
1000 REM Subroutine example.
1010 REM Get yes (Y) or no (N) answer in A$.
1020 INPUT “PLEASE ANSWER YES OR NO: “,A$
1030 IF A$=” “ THEN 1020 \ REM No answer given.
1040 A$=A$(1,1) \ REM Examine 1st char only.
1050 IF A$=”Y” THEN 1070 \ REM OK answer.
1060 IF A$<>”N” THEN 1020 \ REM Not = Yor N either.
1070 RETURN
Page 94
NSB8
Rel. 5.10