next up previous contents index
Next: Handling errors that arise Up: Overview Previous: Overview

Subroutines and diversions

A subroutine, or macro, is simply a sequence of [B/D] commands. We might write subroutines to achieve some particular modularised task, or perhaps simply to break up a program into more manageable components. A number of commands are available to assist in this. In particular, the M:  and RETURN:  commands respectively direct program flow towards and away from a subroutine. The GOTO:  command provides a simple means of jumping from one place in a program to another. Finally, the CLEARRETURNS:  command can be used to override a sequence of nested return addresses from subroutines.

Currently, subroutines are not associated with direct facilities to vary parameters. However, it is possible to write subroutines which, by using strings, constants, and so forth, are reasonably abstract and capable of handling a variety of inputs. As an example, consider the fragment of code shown in Figure 2.1. The subroutine begins with a label, ``@simplesub'', which could be regarded as the name of the subroutine. The subroutine simply calculates the adjustment of one base by another, where the names of the two bases are assumed to be contained in the strings b and d, set outside the subroutine. Having performed the adjustment and output a result, the subroutine terminates. We will assume that these lines of code are contained somewhere in the external file called ``sub.ex'' in the current directory.

To use this subroutine, consider the fragment of code shown in Figure 2.2. This links the external file ``sub.ex'' containing the subroutine to input channel number 3. The following commands then use the subroutine twice, firstly achieving the adjustment of Ubase by Vbase and secondly the adjustment of Xbase by Ybase, where we assume these bases to be to have been defined earlier. Notice that we need not refer to channel 3 explicitly when we call the subroutine. The commands in Figure 2.2 might be issued interactively, or might themselves constitute part of an external file to which you have directed program flow.

  figure1444
Figure 2.1:  Writing a simple subroutine

  figure1489
Figure 2.2:  Using a simple subroutine


next up previous contents index
Next: Handling errors that arise Up: Overview Previous: Overview

David Wooff
Wed Oct 21 15:14:31 BST 1998