What is a module in AS400? Why do we create modules in ILE RPG programming, and why do we need them?
How does a module differ from program and service programs? What are the different types of modules? Do we need modules to run the programs on AS400 systems?
Modules help organize related tasks into smaller code units to be bound into programs or service programs.
A module is a non-running object whose object type is *MODULE. Instead, They are called with either the bound call (CALLB in RPG) or a prototyped call (CALLP in RPG), as the dynamic CALL operation is unavailable for them.
We can develop modules, particularly in large applications, because they allow code reuse. A module may consist of one or more procedures.
A procedure is a function that accepts parameters and returns values.
When using ILE, we must build a module object before creating a program object (*PGM) using the CRTPGM command or a service program object (*SRVPGM) with the CRTSRVPGM object.
During program or service program creation from the module object, all the compiled code is copied into the program or service program object. This code becomes routine/procedure in the program or service program, and there can be primary or sub-procedures. The main method has the same name as the module.
After that, the *Module object is no longer needed. We can delete it anytime if we want.
There are three types of RPG modules.
Module with the cycle-main procedure: The module consists of a cycle-main procedure that includes logic for a complete RPG cycle and zero or more subprocedures. It can be called using a bound call or a program call.
Module with the linear-main procedure: The module consists of a linear-main procedure identified by the MAIN keyword on the control specs and zero or more procedures. It can only be called using a program call. It cannot be called using a bound call.
Module with no main procedure: The module contains only subprocedures and the NOMAIN keyword on the control specs. This type of module does not include logic for the RPG cycle, and it cannot be the program entry module of a program since it has no main procedure. It can only be called using a bound call.