The Call Subroutine (CALLSUBR) command is used in a CL program or procedure for passing control to a subroutine that is defined within the same program or procedure.
The CALLSUBR command has two parameters: Subroutine (SUBR), which contains the name of the subroutine to which control is to be transferred to, and Return value (RTNVAL), which specifies the variable that will contain the return value from the called subroutine. See the following example:
CALLSUBR SUBR(mysubr) RTNVAL(&myrtnvar)
The subroutine mysubr must be defined in the program or procedure by the Subroutine (SUBR) parameter of an SUBR command. The variable &myrtnvar must be defined as TYPE(*INT) LEN(4) so that it contains the value from the Return value (RTNVAL) parameter of either a Return from Subroutine (RTNSUBR) or End Subroutine (ENDSUBR) command found in subroutine mysubr. If no RTNVAL parameter is defined, the return value from the subroutine is ignored.
The CALLSUBR command can be placed anywhere within the program or procedure, including other subroutines, with the exception of a program-level Monitor Message (MONMSG) command. Each CALLSUBR command, when run, places a return address onto the subroutine stack, and the size of the stack can be changed with the use of the Subroutine stack (SUBRSTACK) parameter of the Declare Process Options (DCLPRCOPT) command. If a globally monitored message causes a GOTO command to be run, the subroutine stack will be reset by the next CALLSUBR command that is run.
In the following example, the first CALLSUBR command passes control to the subroutine SUBR1, and the return value of 12 is placed into the variable &myrtnvar when control returns. If a message is monitored by the MONMSG command, the Goto (GOTO) command is run and control branches to the label DUMP. The CL program or procedure is dumped by the DMPCLPGM command , and the next CALLSUBR command to subroutine SUBR2 resets the subroutine stack.
PGM
DCL VAR(&myrtnvar) TYPE(*INT) LEN(4)
MONMSG MSGID(CPF0000) EXEC(GOTO CMDLBL(DUMP))
:
CALLSUBR SUBR(SUBR1) RTNVAL(&myrtnvar)
:
DUMP: DMPCLPGM
CALLSUBR SUBR(SUBR2)
:
SUBR SUBR(SUBR1)
:
ENDSUBR RTNVAL(12)
:
SUBR SUBR(SUBR2)
:
ENDSUBR
ENDPGM