Forums >> Programming >> RPG Programming >>
Using /COPY Members for Prototypes




Posted:
bvstone

Using /COPY Members for Prototypes

 
Using /COPY Members for Prototypes

One thing you most likely noticed (if not, hopefully you read the mention of it) is that the prototype definitions for our first subprocedure is copied both in the module source as well as the program source where the module is used.

This is a perfect place to make use of /COPY members.  

Remember in the first step we created a source physical file named QCOPYSRC?  Well, this is where we're going to place our /COPY members containing the prototypes.  

So, first let's simply copy the member F.MATH from QMODSRC to QCOPYSRC, but we are going to name the member P.MATH (the "P" stands for "Protoype").

CPYSRCF FROMFILE(ILESAMPLE/QMODSRC) TOFILE(ILESAMPLE/QCOPYSRC) FROMMBR(F.MATH) TOMBR(P.MATH)

Now, we can edit the P.MATH member so it contains only the following source (You'll notice that I also added a description that wasn't there before):

 *//////////////////////////////////////////////////////////////*         
 * getSumInt - Return the sum of two integers                   *         
 *//////////////////////////////////////////////////////////////*         
D #getSumInt      PR            10i 0                                     
D  Augend                       10i 0 Const                               
D  Addend                       10i 0 Const                               

We can now also go back and edit our F.MATH module to the following:

H NOMAIN                                                                 
 ****************************************************************        
 * Prototypes                                                   *        
 ****************************************************************        
 /COPY QCOPYSRC,P.MATH                                                   
 *//////////////////////////////////////////////////////////////*        
 * getSumInt - Return the sum of two integers                   *        
 *//////////////////////////////////////////////////////////////*        
P #getSumInt      B                   EXPORT                             
 *--------------------------------------------------------------*        
D #getSumInt      PI            10i 0                                    
D  Augend                       10i 0 Const                              
D  Addend                       10i 0 Const                              
 *--------------------------------------------------------------*        
 /free                                                                   
                                                                         
  return (Augend + Addend);                                              
                                                                         
 /end-free                                                               
P #getSumInt      E  

You'll see under the commented heading of "Prototypes" that we removed the prototype definition for our subprocedure and instead used the /COPY directive so that during compile time the source from the P.MATH member will be pulled in automatically.

Now, lets update or program MATHTEST as well:

  ****************************************************************             
  * Prototypes                                                                 
  ****************************************************************             
  /COPY QCOPYSRC,P.MATH                                                        
  ****************************************************************             
 D Sum             S             10i 0                                         
  ****************************************************************             
  /free                                                                        
                                                                               
   Sum = #getSumInt(20:21);                                                    
                                                                               
   *INLR = *ON;                                                                
  /end-free                                                                    

Again, we simply replaced the prototype definition with the /COPY directive.

Now, we can recreate the F.MATH module, create the MATHTEST module, then create the MATHTEST program:

CRTRPGMOD MODULE(ILESAMPLE/F.MATH) SRCFILE(ILESAMPLE/QMODSRC)

CRTRPGMOD MODULE(ILESAMPLE/MATHTEST) SRCFILE(ILESAMPLE/QRPGLESRC)

CRTPGM PGM(ILESAMPLE/MATHTEST) MODULE(ILESAMPLE/MATHTEST ILESAMPLE/F.MATH)

Now, both our module code and our program code is cleaned up so that it does not contain redundant prototypes.

Next we will look at prefixing the procedure names to make them more descriptive.

 


Last edited 04/03/2015 at 06:20:42


Reply




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).