Programming instructions

35
Intermec Fingerprint 6.13 – Programmer's Guide
5. FINGERPRINT PROGRAMMING, cont'd.
7. Branching to
Subroutines
GOSUB and RETURN
A subroutine is a number of program lines intended to perform a
specific task, separately from the main program execution. Branch-
ing to subroutine can e.g. take place when:
An error condition occurs.
A condition is fulfilled, such as a certain key being pressed or a
variable obtaining a certain value.
A break instruction is received.
Background communication is interrupted.
Another application of subroutines is branching to one and the same
routine from different places in the same program. Thereby, you do
not need to write the routine more than once and can make the
program more compact.
The main instruction for branching to subroutines is the GOSUB
statement. There are also a number of instructions for conditional
branching to subroutines, which will be explained later in this
chapter.
After branching, the subroutine will be executed line by line until
a RETURN statement is encountered.
The same subroutine can be branched to as many times as you need
from different lines in the main program. GOSUB remembers
where the last branching took place, which makes it possible to
return to the correct line in the main program after the subroutine has
been executed. Subroutines may be nested, i.e. a subroutine may
contain a GOSUB statement for branching to a secondary subrou-
tine etc.
Subroutines should be placed on lines with higher numbers than the
main program. The main program should be appended by an END
statement to avoid unintentional execution of subroutines.
Example illustrating nested subroutines:
10 PRINT "This is the main program"
20 GOSUB 1000
30 PRINT "You're back in the main program"
40 END
1000 PRINT "This is subroutine 1"
1010 GOSUB 2000
1020 PRINT "You're back from subroutine 2 to 1"
1030 RETURN
2000 PRINT "This is subroutine 2"
2010 GOSUB 3000
2020 PRINT "You're back from subroutine 3 to 2"
2030 RETURN
3000 PRINT "This is subroutine 3"
3010 PRINT "You're leaving subroutine 3"
3020 RETURN
RUN