[Next] [Up] [Previous]

The SUBROUTINE Statement

The SUBROUTINE statement
(SUBR, SUB)

The SUBROUTINE statement has the same syntax as the FUNCTION statement, except for two differences. The first is that the type of the subroutine itself may not be declared.

The second is that a subroutine may have arguments of type ALTERNATIVE, both within and after the parentheses enclosing the subroutine's regular arguments.

Alternatives after the parentheses are not passed; the subroutine simply returns a 'return code' which indicates which of them is to be used. Thus, a constant giving the maximum number of such arguments allowed can appear after the outer parentheses in the statement, for example,

       SUBROUTINE WTALG((REAL)Q)4

can be called by a statement like

       CALL WTALG(X),{. V=5},10,*,40

and will either fall through, set V equal to 5 and then fall through, or go to either statement 10 or statement 40, depending on the value of the return code.

Arguments of type ALTERNATIVE within the function's calling sequence are declared as such, and if the function has such arguments, and one of these is to be taken upon return from the function, instead of exiting the function with a RETURN statement, use the statement

       RJUMP name

where name is the name of the argument corresponding to the alternative to be taken.

CALL _SELF(...) or CALL subroutine-name(...) can both be used to obtain recursion.

A subroutine, but not a function, can have arguments of type ALTERNATIVE. Such an argument can be any of the possible alternatives available in IF statements, and occupy any position in the argument list. It is, however, necessary to declare those positions in the argument list with an XSUB or PSUB statement.

Another method for allowing a subroutine, upon exit, to transfer control to one of several alternatives is to list them after the closing parenthesis of the argument list. In this case, the 'return code' of the subroutine is used; the position of the alternatives acts as a syntactic indicator that alternatives are present (similar to & in FORTRAN), and does not require declaration.


[Next] [Up] [Previous]