Amos Professional Manual  Contents  Index

Control Structures


GOSUB
structure: jump to a sub-routine
Gosub label
Gosub number
Gosub expression

Packages of program instructions that perform a specific task can be thought of as "routines". When such routines are split into smaller packages of instructions, they can be thought of as "sub-routines". GOSUB is another antiquated command, and is used to perform a jump to a sub-routine. In fact, GOSUB is made redundant by the AMOS Professional procedure system, but it can be useful for STOS users who want to convert programs.

As with GOTO, there are three alternative targets for a GOSUB instruction: labels, line numbers or expressions.

To make sub-routines easier to spot in your program listings, it is good practice to place them at the end of the main program. A statement such as EDIT or DIRECT should also be used to end the main program, which prevents AMOS Professional from executing any GOSUBs after the main program has finished.

RETURN
instruction: return from a sub-routine called by GOSUB
Return

When a program obeys a GOSUB instruction, it must be instructed to RETURN to the main program after the sub-routine has been executed. It should be noted that a single GOSUB statement can be linked to several RETURN commands, allowing exits from any number of different points in the routine, depending on the circumstances. After the RETURN, a jump is made back to the instruction immediately after the original GOSUB. For example:

E> Print "I am the main program"
   For N=1 To 3
    Gosub TEST
   Next N
   End
   TEST:
   Print "Here we go GOSUB" : Wait 50
   Print "Number =";N
   Return

POP
instruction: remove RETURN information
Pop

Normally you cannot exit from a GOSUB statement using a standard GOTO, and this may be inconvenient. For example, there could be an error that makes it unacceptable to return to the

Back    Next
05.04.02