MAIN(main_procedure_name)
The MAIN keyword indicates that this source program is for a linear-main module and contains a linear-main procedure, identified by the main_procedure_name parameter, which will be the program entry procedure for the module.
The main_procedure_name must be the name of a procedure defined in the source program. The linear-main procedure is intended to be called only through the program call interface and not as a bound procedure call; if you make a recursive call to the linear-main procedure, the call will be a dynamic program call.
- If a prototype is specified for the linear-main procedure, it must specify the EXTPGM keyword.
- If a prototype is not specified for the linear-main procedure, and a procedure interface is specified, the procedure interface must specify the EXTPGM keyword.
- If the program has no parameters, and the program is not called from an RPG program, neither a prototype nor a procedure interface is required.
- The procedure cannot be exported; the EXPORT keyword may not be specified on the procedure-begin specification for main_procedure_name.
The following two examples show a linear-main program and its /COPY file.
* The prototype for the linear-main procedure must have
* the EXTPGM keyword with the name of the actual program.
D DisplayCurTime PR EXTPGM('DSPCURTIME')
* The program is named DSPCURTIME, and the module has
* a linear-main procedure called DisplayCurTime.
* The Control specification MAIN keyword signifies that this is
* a linear-main module, and identifies which procedure is the
* special subprocedure which serves as the linear-main procedure,
* which will act as the program-entry procedure.
H MAIN(DisplayCurTime)
* Copy in the prototype for the program
/COPY DSPCURTIME
*--------------------------------------------------
* Procedure name: DisplayCurTime
*--------------------------------------------------
P DisplayCurTime B
D DisplayCurTime PI
/FREE
dsply ('It is now ' + %char(%time()));
/END-FREE
P DisplayCurTime E
The following example shows a linear main program that does not require a prototype. The program is named PRTCUSTRPT, and the module has a linear-main procedure called PrintCustomerReport. The program is intended to be the command processing program for a *CMD object, so there is no need for an RPG prototype. The Control specification MAIN keyword signifies that this is a linear-main module, and identifies which procedure is the special subprocedure which serves as the linear-main procedure, which will act as the program-entry procedure.
H MAIN(PrintCustomerReport)
*--------------------------------------------------
* Program name: PrintCustomerReport (PRTCUSTRPT)
*--------------------------------------------------
P PrintCustomerReport...
P B
F ... file specifications
D PI EXTPGM('PRTCUSTRPT')
D custName 25A CONST
... calculations, using the custName parameter
P PrintCustomerReport...
P E