Blog Archive

RPGLE free format subprocedure example code - AS400 (iSeries)

Subprocedure consist of the following
  • Begin and End
  • May or may not return value
  • Have access to global and local variables
  • Any variables inside the definition are considered local
  • To return complex data use data structure

Sample RPGLE program with subprocedure

h dftactgrp(*no)                                  
                                                  
d MyProcedure     PR            52a               
d                               30a               
d                                3s 0             
                                                  
d $name           s             30a   inz('as400') 
d $age            s              3s 0 inz(21)     
d $message        s             52a               
                                                  
 /free                                            
                                                  
    $message = MyProcedure($name:$age);           
    dsply $message;                               
                                                  
    eval *inlr = *on;                             
    return;                                       
                                                  
 /end-free                                        
                                                  
p MyProcedure     B                   EXPORT      
                                                  
d  MyProcedure    PI            52a                
d   $name                       30a                         
d   $age                         3s 0                       
                                                            
d $message        s             52a                         
                                                            
 /free                                                      
                                                            
    $message = 'My name is ' + %trim($name) + ', ' +        
              'age ' + %trim(%editc($age:'Z')) + ' years.'; 
    return $message;                                        
                                                            
 /end-free                                                  
                                                            
p MyProcedure     E 

Tip: You can code one or more subprocedures in a module without coding a main procedure. Such a module is called a NOMAIN module, since it requires the specification of the NOMAIN keyword on the control specification. When there is no main procedure, no cycle code is generated for the NOMAIN module.