IBM i e-Book
A Developer’s Guide to Mastering IBM i Concepts
IBM i Index
Control Language (CL)
ILE with CL using Procedures
Introduction
In CLLE, subroutines and procedures are essential programming constructs used to organize and modularize code for better maintainability and reusability. Subroutines and procedures in CLLE provide a way to organize code, promote code reuse and improve the overall structure of the program. Let’s know about these concepts briefly below.
Subroutines
A subroutine is a self-contained section of code within a CLLE program that is defined to perform a specific task or set of related tasks. It has a unique name that allows the program to call and execute the subroutine as needed. This helps the developer of the CLLE program to reuse the code as many times as they need in their code without having to rewrite the same code for the functionality again and again.
Subroutines in CLLE
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. Let’s look at these commands one by one along with the two other important commands which are widely used when using subroutines in CLLE.
- SUBR: The SUBR command is used in the CLLE program to mark the beginning of the subroutine block. It has the following syntax:
SUBR SUBR(Subroutine_Name)
- ENDSUBR: The ENDSUBR command is used in the CLLE program to mark the termination of the subroutine block that began with the SUBR command.
- RTNSUBR: The optional RTNSUBR command is used to return a value and exit the subroutine that has been called.
RTNSUBR RTNVAL(INTVALUE)
- CALLSUBR: The CALLSUBR command can be used anywhere in the CLLE program to call the subroutine block and executes the code written inside it. The CALLSUBR command has the following syntax:
CALLSUBR SUBR(Subroutine_Name)
Subroutines are physically located in your source code following your main program logic and prior to the ENDPGM command. You can have many subroutines within your program, each delimited by paired SUBR and ENDSUBR commands.
The SUBR command has one parameter, SUBR, which is used to name the subroutine. This name is then used with the SUBR parameter of the CALLSUBR command to identify which subroutine is to be run. The ENDSUBR command defines the end of the common logic that started with the previous SUBR command. The ENDSUBR command has one parameter, RTNVAL, which can be used to specify a return value that can be returned to the caller of the subroutine.
The CALLSUBR command has two parameters: SUBR, which identifies the subroutine to call, and RTNVAL, which can optionally identify a CL variable to receive the return value of the called subroutine. There is also a Return from subroutine command, RTNSUBR, which can be used to immediately return control to the calling CALLSUBR command without having to run the ENDSUBR command. The RTNSUBR command also has one parameter, RTNVAL, which, like ENDSUBR, allows you to identify a return value to be returned to the calling CALLSUBR.
Example of CLLE program:
Let’s look at the below example of the CLLE program that uses a subroutine inside it.
CLLE Snippet showing subroutine.
In the above CLLE source snippet we have used the subroutine to perform a basic arithmetic addition operation on two numbers namely &NUM1 and &NUM2 and we are storing the result of the two numbers into &SUM.
In the above example we have used the CALLSUBR command to call the subroutine ADDSUBR. This subroutine is defined between the lines 011.00 and 012.00 as shown above. Inside the subroutine definition we have written the logic of the sum using the CHGVAR command.
We can use this subroutine as many times as we want and hence it increases the reusability of the code written inside the subroutine definition.
Procedures
In Integrated Language Environment, procedures are modular units of code that encapsulate a set of operations or logic ILE is an architectural framework used in IBM’s AS/400 and IBM i series systems allowing for integration of different programming languages like RPG. The procedure definition involves specifying the details and structure of a procedure in programming language and its fundamental step in creating a modular and usable piece of code that can be called or invoked from other parts of a programs.
The sub procedures are just like functions that we use in modern programming languages. There is a difference between subroutines and procedures as follows:
- You can pass parameters to a subprocedure, even passing by value.
- The parameters passed to a subprocedure and those received by it are checked at compile time for consistency. This helps to reduce run-time errors, which can be more costly.
- Names defined in a subprocedure are not visible outside the subprocedure.
- You can call subprocedures recursively.
- You can call the subprocedure from outside the module, if it is exported.
Procedures in CLLE
A CL procedure is a group of CL commands that tells the system where to get input, how to process it, and where to place the results. The procedure is assigned a name by which it can then be called by other procedures or bound into a program and called. As with other kinds of procedures, you must enter CL procedure source statements, compile, and bind them before you can run the procedure.
CL procedures can be written for many purposes, including:
- To control the sequence of processing and calling of other programs or procedures.
- To display a menu and run commands based on options selected from that menu. This makes the workstation user’s job easier and reduces errors.
- To read a database file.
- To handle error conditions issued from commands, programs, or procedures, by monitoring for specific messages.
- To control the operation of an application by establishing variables used in the application, such as date, time, and external indicators.
- To provide predefined functions for the system operator, such as starting a subsystem or saving files. This reduces the number of commands the operator uses regularly, and it ensures that system operations are performed consistently.
In CLLE, the CALL command is used to call the program whereas to call the procedure we use the CALLPRC command.
Example of CLLE program:
Let’s look at the below example of the CLLE program that uses a sub-procedure inside it.
CLLE Snippet showing sub-procedure:
The above CLLE program as we can see, uses the CALLPRCÂ command to call the external procedure named MERGPROC with the VALUE1 and VALUE2 as the parameters.
According to the above CLLE snippet the parameters passed in the CALLPRC command would have the following values:
VALUE1 =Â ROHAN
VALUE2 =Â SINGH
Let’s check the source of the module containing the MERGPROC:-
As we can see above the logic of the concatenation of the values passed from the CLLE program has been written in the source. This procedure returns the concatenated value at the end of program to the CLLE program we saw above and displays the output as shown: