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.

Therefore, the following rules apply:
  • 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.
A linear-main module will not include logic for the RPG program cycle; thus language features dependent on the cycle may not be specified.
Note: The NOMAIN keyword also allows you to create a module that does not contain the RPG program cycle.
See Linear Module for more information.

The following two examples show a linear-main program and its /COPY file.

Figure 1. /COPY file DSPCURTIME used in the following sample linear-main program
      *  The prototype for the linear-main procedure must have
      *  the EXTPGM keyword with the name of the actual program.
     D DisplayCurTime  PR                  EXTPGM('DSPCURTIME')
Figure 2. A sample linear-main procedure used in a program
* 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.

Figure 3. A linear main program that is not intended to be called from within any RPG program or 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