Writing a Subroutine

A subroutine is a series of instructions that an exec invokes to perform a specific task. The instruction that invokes the subroutine is the CALL instruction. The CALL instruction may be used several times in an exec to invoke the same subroutine.

When the subroutine ends, it can return control to the instruction that directly follows the subroutine call. The instruction that returns control is the RETURN instruction.

IKJC3006

Subroutines may be internal and designated by a label, or external and designated by the data set member name that contains the subroutine. The preceding example illustrates an internal subroutine named "sub1".

Note:
Because internal subroutines generally appear after the main part of the exec, when you have an internal subroutine, it is important to end the main part of the exec with the EXIT instruction.

The following illustrates an external subroutine named "sub2".

IKJC3007
To determine whether to make a subroutine internal or external, you might consider factors, such as:
  • Size of the subroutine. Very large subroutines often are external, whereas small subroutines fit easily within the calling exec.
  • How you want to pass information. It is quicker to pass information through variables in an internal subroutine. This method is described in Passing Information by Using Variables.
  • Whether the subroutine might be of value to more than one exec or user. If so, an external subroutine is preferable.