Unlike cycle main and linear main programs, nomain modules have no main procedure.
Editor’s note: This article is excerpted from Programming in ILE RPG, Fifth Edition, chapter 14, “Building Modular Programs with Procedures.”
The programs previously discussed [cycle main and linear main] are complete programs in themselves. They include a main procedure, along with all the other required subprocedures, coded within one source member, or compile unit. ILE also enhances modular programming concepts by allowing you to code a source member that consists of a segment of a program without a main procedure. The resulting Nomain module contains only subprocedures (one or more) that you can combine with other modules to create a program. By itself, a Nomain module cannot create a program object. One of the modules in a program must have a main procedure.
A Nomain module includes a Ctl-opt instruction with the Nomain keyword. To see how to code a Nomain module, let’s separate the example Celsius subprocedure from the rest of the program that uses it. The highlighted code illustrates the necessary changes:
The primary change in this example is the inclusion of the Nomain control option. This keyword tells the compiler that the source code does not contain a main procedure. Instead, the source consists only of global declarations and subprocedures (just one, Celsius, in this case).
The only other change in this code is the addition of the Export keyword on the Dcl-proc instruction. The Export keyword allows the procedure to be called from outside this module. When you combine this module with others to create a program, procedures in those other modules can execute the Celsius procedure, even though they don’t contain the code for the procedure. The calling procedures need not have any special coding to execute an exported procedure. Without Export, the Celsius procedure can only be executed from a procedure within this module. Most procedures in Nomain modules are exported to other modules and include the Export keyword.
Nomain modules allow the ILE RPG programmer to fully exploit modular programming concepts. Code that is common to many programs can be isolated as a procedure in a Nomain module and then reused many times by any program that needs it. In addition, Nomain modules can eliminate redundant code in an application, making the application easier to maintain and more reliable than if the application had multiple copies of the same code scattered among many programs. Nomain modules also can help enforce business rules and practices in an organization by centralizing application functions in a common library of modules for use and reuse throughout the organization.