How can we help you?

We have hundreds of highly-qualified, experienced experts working in 70+ technologies.

IBM i e-Book

A Developer’s Guide to Mastering IBM i Concepts

Integrated Language Environment

Procedure

A main procedure is one that takes control when an ILE program is called and can be specified as the program entry procedure.The main source section contains the set of H, F, D, I, C, and O specifications that start a module and define a cycle-main procedure.

The main procedure is specified by everything that comes before the first procedure specification, so no special coding is required. The global Definition specifications allow for the coding of the main procedure’s parameters using either a prototype and procedure interface or a *ENTRY PLIST in the main procedure’s calculations.

It is assumed that the procedure interface for the main procedure is any interface for procedures found in the global definitions. The prototype with the same name must come before the procedure interface in the source, and the name is required for the procedure interface for the main procedure.

The name of the module that is being created and the main procedure must match. Either use this name for the prototype and procedure interface or include it in the prototype’s EXTPROC keyword.

Sample code:

Ctl-Opt DftActGrp(*No) Main(MainProc);

Dcl-proc MainProc;

    Dcl-s String char(30);

    String = 'This is MainProc';

    *Inlr = *On;

End-proc;

To define the main procedure as a program, you can also use a prototype and procedure interface. In this case, the prototype’s EXTPGM keyword would be specified.


Sample code:

Dcl-Pr CheckObj extpgm('CHECKOBJ');
   Object  char(10);
   Library char(10);
   Found   ind;
End-pr;

CheckObj(ObjectName:Library:Found);
Dcl-Pi CheckObj;
   Object  char(10);
   Library char(10);
   Found   ind;
End-Pi;

If Found = '1';
   Dsply 'Object found';
Else;
   Dsply 'Object not found';
Endif;

Subprocedure:

A procedure that follows the main source section is called a subprocedure. subprocedure differs from a main procedure primarily in that:

  • Names that are defined within subprocedure are not accessible outside the subprocedure.
  • No cycle code is generated for the subprocedure.
  • The call interface must be prototyped.
  • Calls to subprocedures must be bound procedure calls.
  • Only P, F, D, and C specifications can be used.
  • Other than being called through a program call rather than a bound call, a linear-main procedure is just like any other subprocedure.

Because the data items in subprocedures are local, they can offer independence from other procedures. Typically, local data items are kept in automatic storage, which means that the value of a local variable is not preserved between calls to the procedure.

Another feature provided by subprocedures. A subprocedure can be called in an expression to return a value, and parameters can be passed to it by value.

Below figure illustrates the possible layout of a module with multiple procedures:

Subprocedure Definition:


Sample code:

Dcl-Proc Calculator Export;

    Dcl-Pi Calculator Zoned(10:0);
        Num1 Zoned(4:0);
        Num2 Zoned(4:0);
        Num3 Zoned(4:0);
    End-Pi;

    Dcl-S Result Zoned(10:0);

    Result = Num1 * 10 + Num2 + Num3 - 15;
    Return Result;

End-Proc;

Dcl-s Number1 Zoned(4:0) Inz(20);
Dcl-s Number2 Zoned(4:0) Inz(30);
Dcl-s Number3 Zoned(4:0) Inz(40);
Dcl-s Result Zoned(10:0);

    Dcl-pr Calculator Zoned(10:0);
       Num1 Zoned(4:0);
       Num2 Zoned(4:0);
       Num3 Zoned(4:0);
    End-Pr;

Result = Calculator(Number1:Number2:Number3);
  • A prototype that includes the name, any parameters, and any return value.
  • “Dcl-Proc” keyword will begin a procedure.
  • A definition of a Procedure-Interface which defines any parameters and the return value. The corresponding prototype and the procedure interface must match. If the subprocedure does not return a value and receives no parameters, the procedure-interface definition is not required.
  • Additional definition specifications for prototypes, constants, and variables required by the subprocedure. These definitions are local definitions.
  • Any standard or free form calculation specifications required to complete the procedure’s task. Both local and global definitions may be used in the calculations. The subprocedure contains any local subroutines. They are only useful within the subprocedure. A RETURN operation must be included in the subprocedure if it returns a value.
  • The “End-Proc” Keyword indicates the end of a procedure.

Subprocedure Scope:

A subprocedures defined items are all local. When a local item and a global data item is defined with the same name, the local definition is used for all references to that name within the subprocedure.

  • Subroutine names and tag names are known only to the procedure, even those defined in the main procedure, in which they are defined.
  • Every field specified in the specifications for the input and output is global. Even if there is a local variable with the same name, the global name is used when a subprocedure uses input or output specifications (for example, while processing a read operation).

Subprocedure calculations:

A subprocedure does not have its own RPG cycle code, so it needs to be coded differently than a main procedure. When one of the following happens, the subprocedure ends:

  • A RETURN operation is processed.
  • The final computation within the subprocedure is processed.

How can we help you?

We have hundreds of highly-qualified, experienced experts working in 70+ technologies.

share_iconShare
X

Awards and Certifications

company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo