OptionalParameters

From MidrangeWiki
Jump to: navigation, search

ILE CL programs can accept optional parameters. There are two methods for handling optional parameters in ILE CL programs — implicit and explicit.

Implicit optional parameter handling employs the MONMSG command to trap the MCH3601 exception which results when missing parameters are referenced directly. In the following example note that the default value of the parameter variable is not lost during CHGVAR processing if the optional parameter was not supplied.

            PGM        PARM(&OPTRUN) /* optional parameter */
            
   /* optional parameter definitions */
            DCL        VAR(&OPTRUN)  TYPE(*CHAR) LEN(10)
            
   /* parameter variable definitions */
            DCL        VAR(&RUNOPT)  TYPE(*CHAR) LEN(10) VALUE('*PRELIM')
            
   /* receive optional parameters */
            CHGVAR     VAR(&RUNOPT) VALUE(&OPTRUN) /* optional run option */
            MONMSG     MSGID(MCH3601) EXEC(DO) /* parm not passed */
               RCVMSG     MSGTYPE(*LAST) RMV(*YES) /* remove escape +
                            message from job log */
            ENDDO

On the other hand, explicit optional parameter handling requires a call to a CEE API to predetermine the presence or absence of an optional parameter. Use the CEETSTA procedure to determine if the parameter has been passed. Note that all CEE APIs are bound in by default and, thus, are always available.

            PGM        PARM(&PARM1 &PARM2)
            
            DCL        VAR(&PARM1) TYPE(*CHAR) LEN(3)
            DCL        VAR(&PARM2) TYPE(*CHAR) LEN(3)
            DCL        VAR(&PARMRESULT) TYPE(*INT) LEN(4)
            DCL        VAR(&PARMNUMBER) TYPE(*INT) LEN(4)
            
            CHGVAR     VAR(&PARMNUMBER) VALUE(2)
            CALLPRC    PRC(CEETSTA) PARM(&PARMRESULT &PARMNUMBER *OMIT)
            IF         COND(&PARMRESULT *EQ 1) THEN(DO)
               CALL PGM(PROGRAM2) PARM(&PARM1 &PARM2)
            ENDDO
            ELSE       CMD(DO)
               CALL PGM(PROGRAM2) PARM(&PARM1)
            ENDDO
            
            ENDPGM

Categories