Chapter 10: Procedures


The RPG IV language provides a rich set of features that allow the programmer to modularize any application. Part of that feature set is the user-written function or, more properly, the procedure. Procedures are independent processes that typically are called to perform a specific task. The terms procedure and subprocedure are used interchangeably in RPG.

USER-WRITTEN PROCEDURES

When a procedure is written, it is referred to simply as a procedure. When a higher-level process calls the procedure, it becomes a subprocedure of that higher-level process. The main entry point into a program is referred to as the mainline procedure. All other procedures within the RPG program are referred to as subprocedures.

Header, definition, and procedure specifications are used to create procedures for RPG programs. The actual work of a procedure is performed by calculation specifications.

The procedure specification is used to name and delineate procedures within the RPG source code. A source file can contain only a mainline procedure, a mainline procedure and one or more subprocedures, or subprocedures only. When a source file is made up of only subprocedures, a header specification should be specified with the NOMAIN keyword. This ensures that the RPG program cycle code is not inserted into the compiled *MODULE. If NOMAIN is not specified, the procedures work the same as if NOMAIN were specified, but the overall module size is increased unnecessarily.

Procedures can exist in any number of program modules. A program is made up of one or more modules. Each module can export any of its subprocedures. Exported subprocedures can be called by procedures in other modules within the same program. This process is shown in Figure 10.1.

click to expand
Figure 10.1: Procedures stored in separately compiled modules.

The logical path followed when a subprocedure is evoked is shown in Figure 10.1. In reality, the independent modules are statically bound into a single program object.

When a source file is compiled, the compiler generates a module object. On the AS/400, the CRTRPGMOD is used to generate a module. To create a program, one or more modules are bound together by the program binder. On the AS/400, the CRTPGM command evokes the program binder.

Figure 10.2 illustrates the structure of a source file that contains a mainline procedure and two subprocedures.


Figure 10.2: Source file structure with mainline procedure.

RPG program source code is organized in modules. A module can be a stand-alone program, a mainline procedure, or a procedure module. A stand-alone program is all-inclusive. It contains all the program statements required to perform its task.

A mainline procedure contains the process logic, support code, and RPG program cycle. This includes database file declarations and, perhaps, the user-interface components. A mainline procedure typically calls subprocedures, either statically or dynamically, to perform the detail work of the various processes.

A procedure module contains subprocedures that are called by higher-level programs or procedures. Typically, these subprocedure modules are one of two types, as follows:

  • Application-Specific Subprocedures. Application-specific subprocedures are unique to the application in which they are bound.

  • General-Purpose Subprocedures. General-purpose subprocedures perform more programmatic tasks such as converting a character string to all uppercase letters or formatting a date into a textual representation.




The Modern RPG IV Language
The Modern RPG IV Language
ISBN: 1583470646
EAN: 2147483647
Year: 2003
Pages: 156
Authors: Robert Cozzi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net