Subroutines in CL
The Subroutine (SUBR) command is used in a CL program or procedure, along with the End Subroutine (ENDSUBR) command, to delimit the group of commands that define a subroutine. The name of the subroutine, as used by the CALLSUBR command, is defined by the SUBR parameter on the SUBR command.
Return Subroutine (RTNSUBR)
PGM
CallSubr subr(Validate)
SUBR Validate
Chgvar &Errors 0
Chkobj &epBldPgm (*Pgm)
Monmsg CPF9800 exec(Do)
chgvar &Errors -1
chgvar &Errormsg ('Report Build pgm:' *bcat %Trim(&epBldpgm) +
*bcat ' does not exist')
Rtnsubr
EndDo
Chkobj &epAPILib (*Lib)
Monmsg CPF9800 exec(Do)
chgvar &Errors -1
chgvar &Errormsg ('API library:' *bcat %Trim(&epAPILib) +
*bcat ' does not exist')
Rtnsubr
EndDo
ENDSUBR
EndPgm
The following example is about the general structure of a CL procedure that contains a subroutine.
PGM
DCLPRCOPT SUBRSTACK(25)
DCL VAR(&RTNVAR) TYPE(*INT) LEN(4)
:
CALLSUBR SUBR(SUBR1) RTNVAL(&RTNVAR)
:
SUBR SUBR(SUBR1)
:
RTNSUBR RTNVAL(-1)
:
ENDSUBR
ENDPGM
In this example, the Declare Processing Options (DCLPRCOPT) command was used to specify the size of the subroutine stack to be 25. The variable &RTNVAR is used to contain the return value from the subroutine. The CALLSUBR command will transfer control to the subroutine SUBR1, as defined by the SUBR command. If the RTNSUBR command is run, the value of &RTNVAR will be -1, if the ENDSUBR command is run, &RTNVAR will equal 0. If no RTNVAL parameter was defined on the CALLSUBR command, the return value from the subroutine would be ignored.