Forums >> Programming >> RPG Programming >>
Using Our First RPG ILE Module


Using Our First RPG ILE Module

Using Our First RPG ILE Module

Now that we have created our first ILE module, we will want to use it!

Below is the source code for a program named MATHTEST that we placed in QRPGLESRC:

 * Prototypes                                                                     
D #getSumInt      PR            10i 0                                             
D  Augend                       10i 0 Const                                       
D  Addend                       10i 0 Const                                       
D Sum             S             10i 0                                             
  Sum = #getSumInt(20:21);                                                        
  *INLR = *ON;                                                                    

As you can see, this program is very short and simple.  All we are doing is getting the sum of 20 plus 21.

You will see that in order to use our new function #getSumInt we need to define the prototype in the D-Spec our application.  You've probably noticed that the prototype we using is exactly the same as the prototype that we used in the F.MATH module source, and you would be right.  This is a case where we just copied the source from one member to another.  Not the best practice, but one we are all used to.  Later we'll show how to use /COPY books to update this.

Now, to compile this program will take two steps.  

  • Create a module named MATHTEST
  • Create a program named MATHTEST.  This program will consist of two modules, MATHTEST and F.MATH.

The first command we will run to create the MATHTEST module is as follows:


Once our module is created, we now will create a program from the two modules, MATHTEST and F.MATH.


What we have just done is created a program from two separate modules.  This method is called "Bind By Copy".  This means that a copy of the module is actually "bound" to the program.  If we were to update the module, for example adding a new subprocedure, our MATHTEST program would still contain a copy of the old F.MATH module until it is recompiled at which time the new copy of the module would be bound to the program.  

If we were to run the following command:


We would see in the *MODULE detail the two modules that create this program:

                         Display Program Information 
                                                                Display 3 of 7 
Program  . . . . . . . :   MATHTEST      Library  . . . . . . . :   ILESAMPLE  
Owner  . . . . . . . . :   BVSTONE    
Program attribute  . . :   RPGLE      
Detail . . . . . . . . :   *MODULE    

Type options, press Enter. 
  5=Display description   6=Print description 

                                         Creation  Optimization  Debug 
Opt  Module      Library     Attribute   Date         Level      Data 
     MATHTEST    ILESAMPLE   RPGLE       03/26/15  *NONE         *NO        
     F.MATH      ILESAMPLE   RPGLE       03/26/15  *NONE         *NO        

We can now call our program just as we would any other program.

It's interesting to note that for a while we've been doing this same process, but only with one module.  When we compile RPGLE programs using option 14 (or CRTRPGPGM) in the background the system first creates a module with the program name in library QTEMP, then creates a program with that single module.  The module is then removed from the system as it is no longer needed.   Try running the DSPPGM command above on a "normal" RPGLE program.

If we debug our program we can see how things work by adding a break point on the call to the #getSumInt procedure:

                            Display Module Source 

Program:   MATHTEST       Library:   ILESAMPLE      Module:   MATHTEST   
     1        **************************************************************** 
     2        * Prototypes                                                     
     3        **************************************************************** 
     4       D #getSumInt      PR            10i 0                             
     5       D  Augend                       10i 0 Const                       
     6       D  Addend                       10i 0 Const                       
     7        **************************************************************** 
     8       D Sum             S             10i 0                             
     9        **************************************************************** 
    10        /free                                                            
    12         Sum = #getSumInt(20:21);                                        
    14         *INLR = *ON;                                                    
    15        /end-free                                                        
Debug . . . eval sum

F3=End program   F6=Add/Clear breakpoint   F10=Step   F11=Display variable 
F12=Resume       F17=Watch variable   F18=Work with watch   F24=More keys 
SUM = 41                                                                       

While a simple example, this should show you just how easy it is to create reusable code with RPG and ILE.  

But, with every lesson that starts at the lowest level, before we start creating things for production we should start applying "best practice" methods.  

I only put best practice in quotes since those can vary from person to person, but I will share what I feel are best practices and have served me well for over 15 years.  And trust me, I have some code out there I wish I could go and and update, as I'm sure we all do.  So this is really going to go through all my mistakes and bring us close to the point where we should be now.

Next... /COPY members for prototypes.

Last edited 03/29/2015 at 16:04:17


Copyright 1983-2017 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).