next up previous contents
Next: SUBROUTINE, CALL and RETURN Up: Subprograms Previous: Subprograms

Introduction

As progams get longer, they get harder to write and maintain. Once they have reached roughly 100 lines of code it is time to split them into small units called SUBPROGRAMS or ROUTINES. The routines can be coded and compiled completely independently of one another. They are combined into a single program using LINK. When the program runs, processing starts with the main routine. To execute code in another routine the MAIN routine makes a call to it. When complete the called routine returns control to the instruction after the one making the call. Routine calls may be nested, but routines may not directly or indirectly call themselves. A routine can be called from any number of different points within other routines. This leads to a second reason for breaking up a progam - to save coding time and program size by identifying program sequences that are being duplicated and storing them in a routine. Each routine can have its own LOCAL VARIABLES. These are variables and arrays held with the routine - two routines can use the same names for local variables without interfering with each other. There are two methods for sharing data between routines. One is via COMMON blocks (see section 14) and the other is via the argument list set up when a routine is called. At this point the calling routine can allow the called routine to access certain of its own local variables. There are 4 statements than can be used to introduce subprograms. 3 of them: SUBROUTINE, FUNCTION and PROGRAM, are discussed in this section and the last BLOCK DATA is described in section 16.2


next up previous contents
Next: SUBROUTINE, CALL and RETURN Up: Subprograms Previous: Subprograms
n west (APC)
2000-03-08