7 Subprograms

Subprogram Considerations

Temporary Default States

The computer enters a new operating environment when entering each subroutine or function subprogram. The current environment is suspended until exiting the subprogram. The following default states are set when a subprogram environment is entered:

Upon return to the main program, all of the above are restored to their previous states.

Adding and Deleting Subprograms

There are two ways to add a new subprogram to a program. It may either replace an existing subprogram or come after all other subprograms.

In order to delete the first line of a subprogram, the SUB or DEF FN statement, the entire subprogram must be deleted. To delete an entire subprogram, use the DEL SUB or DEL FN statements:

DEL SUB subprogram name [TO END]


DEL FN function name [$] [TO END]

Each statement deletes the named subprogram or function from memory. If TO END is specified, all successive subprograms and functions are also deleted.

The SUB statement can be edited as long as it remains a SUB statement or is changed to a DEF FN.

Using COM Statements

Values can also be passed to a subprogram with a COM statement. The list of items in the subprogram COM must be a subset of the main program COM statement; that is, it must match to some point in the main program COM.

Here are some valid examples:

20   COM A(4,4),B,INTEGER C,D(3,3),E$[28],F$(2,4)[56]
150  END
160  SUB Payroll
170     OPTION BASE 1
180     COM X(*),Y,INTEGER Z,Q(1:3,1:3)
220     SUBEND
230  DEF FNAccounts(X,Y,Z)
240     COM I(1:4,1:4)
Here is an invalid example using the same main program COM statement:

300   SUB Total
310     COM M(4,4),N
350   SUB Price
360     OPTION BASE 1                There is no item corresponding
370     COM L(4,4),M,Q(3,3)          to array Q, causing error 47.
Arrays can be specified in a subprogram COM statement using an array identifier (see line 180 above). A variable cannot be an item in a subprogram COM statement, however, if it is also a formal parameter. For example, if the following is executed, Error 12 occurs:

400   SUB Sub(X,Y,Z$)
410     COM A(2,2),X
Subprograms may also consist of any of the other variable-declarative statements--DIM, REAL, SHORT, INTEGER and DINTEGER. The variables declared, however, may not be in the subprogram COM statement or the formal parameter list.

Here is a valid example:

450   SUB X(X,Y(*),Z$,A)
460     COM B(3,3),C$[20],D,SHORT E
470     DIM F(5,2),G$(2,2)[50]
480     SHORT H,I(3,7,2)
Here is an invalid example:

520   DEF FNTaxes(A,B(*),C$,D)
530     COM E(3,4),INTEGER F
540     DIM C$[20],E(2,2)
C$ and E were already defined, so ERROR 12 IN LINE 540 WITH C$ will occur.

All variables in a subprogram that are not part of the formal parameter list or the COM statement are known as local variables and cannot be accessed from any other program segment. Storage of local variables is temporary; the memory space is returned to user read/write memory upon return to the calling program. All variable names in a subprogram are independent of variables with the same name in other program segments or other subprograms.

File numbers can be passed to a subprogram in the parameter list. For example, the following statements assign DATA to file number 3:

10   ASSIGN #1 TO "DATA"
20   CALL Routine(#1)
60   SUB Routine(#3)
Any operations, such as PRINT#, which involve file #3 in the above subprogram will affect file #1 (DATA) in the calling program. The data pointers in file #1 are maintained in file #3; then the status of file #3 is passed to file #1, and vice versa, when the subroutine is exited.

100   CALL X(#4)
140   SUB X(#2)
150      ASSIGN #2 TO "Payroll"
When control returns to the calling program, file #4 is still assigned to the file Payroll.

A file can also be implicitly buffered in this manner:

200   CALL Data(#4)
240   SUB Data(#2)
250      ASSIGN #2 TO "Payroll"
When control returns to the calling program, file #4 is still assigned to Payroll and still buffered.

If a file is opened in a subprogram, but not passed as a parameter or in COM, it is automatically closed upon return to the calling program. See page 195 for an explanation of ASSIGN.

Note that if a variable in a COM declaration is wrong, any variables which follow will also be disregarded.

Eloquence Language Manual - 19 DEC 2002