Procedures and subroutines in CL400
Procedures and subroutines in CL400 are modular code blocks that allow you to break down a large program into smaller, more manageable pieces. These modular code blocks can be defined once and then called multiple times from different parts of the program, making it easier to develop, maintain and reuse code. Here is an explanation of how to define and call procedures and subroutines in CL400, along with some code examples:
- Defining procedures and subroutines:
To define a procedure or subroutine, you need to use the PARM keyword to define any input and output parameters, followed by the name of the procedure or subroutine, and then the statements that make up the procedure or subroutine. Here is an example of how to define a simple procedure:
/* Define a procedure */
PGM &PARM1
CHGVAR VAR(&PARM1) VALUE('Hello, World!')
SNDPGMMSG MSG(&PARM1) TOUSR(*CURRENT)
ENDPGM
In this example, the PGM keyword is used to define a procedure named &PARM1. The CHGVAR and SNDPGMMSG statements are the statements that make up the procedure.
- Calling procedures and subroutines:
Once you have defined a procedure or subroutine, you can call it from other parts of your program using the CALL statement. You can also pass any necessary input and output parameters to the procedure using the PARM keyword. Here is an example of how to call the procedure defined above:
/* Call a procedure */
CALL PGM(MYLIB/MYPROC) PARM(&MESSAGE)
In this example, the CALL keyword is used to call the procedure named MYPROC, which is located in the MYLIB library. The &MESSAGE variable is passed as an input parameter to the procedure.
- Defining subroutines:
To define a subroutine, you need to use the SUBR keyword instead of the PGM keyword. Here is an example of how to define a simple subroutine:
/* Define a subroutine */
SUBR &PARM1
CHGVAR VAR(&PARM1) VALUE('Hello, World!')
SNDPGMMSG MSG(&PARM1) TOUSR(*CURRENT)
ENDSUBR
In this example, the SUBR keyword is used to define a subroutine named &PARM1. The CHGVAR and SNDPGMMSG statements are the statements that make up the subroutine.
- Calling subroutines:
To call a subroutine, you need to use the CALL statement in the same way as for a procedure. Here is an example of how to call the subroutine defined above:
/* Call a subroutine */
CALL MYLIB/MYSUBR PARM(&MESSAGE)
In this example, the CALL keyword is used to call the subroutine named MYSUBR, which is located in the MYLIB library. The &MESSAGE variable is passed as an input parameter to the subroutine.
Overall, procedures and subroutines in CL400 are powerful tools that allow you to break down a large program into smaller, more manageable pieces. By defining and calling these modular code blocks, you can make your programs easier to develop, maintain and reuse.
Leave a Comment