Difference between revisions of "Procedure Pointers"

From MidrangeWiki
Jump to: navigation, search
(New page: Procedure pointers are a way to give a program a unique level of flexibility in the way logic is invoked. Basically, it gives you the ability to store a procedure reference in a p...)
 
m
Line 39: Line 39:
 
The [[prototype]] for each procedure must be consistent with the prototype associated with the procedure pointer; that is, they must accept the same parameters. But you can include the [[OPTIONS(*NOPASS)]] and/or [[OPTIONS(*OMIT)]] keywords to make some parameters optional or omissible.
 
The [[prototype]] for each procedure must be consistent with the prototype associated with the procedure pointer; that is, they must accept the same parameters. But you can include the [[OPTIONS(*NOPASS)]] and/or [[OPTIONS(*OMIT)]] keywords to make some parameters optional or omissible.
  
(Copied from http://www.bmeyers.net/2005/09/using-callp-with-a-procedure-pointer.php)
+
(Portions copied from http://www.bmeyers.net/2005/09/using-callp-with-a-procedure-pointer.php)
  
 
[[Category:RPG]]
 
[[Category:RPG]]

Revision as of 18:49, 30 March 2007

Procedure pointers are a way to give a program a unique level of flexibility in the way logic is invoked.

Basically, it gives you the ability to store a procedure reference in a pointer variable ... and then invoke that procedure in your code.

The following is a basic example of how to use a procedure pointer:

  // Possible procedures to call
D Addcust         PR                                  
D                                5U 0 Value           
D Updcust         PR                                  
D                                5U 0 Value           
D Delcust         PR                                  
D                                5U 0 Value           

  // Prototype for generic procedure
D Process         PR                  Extproc(Processptr)
D                                5U 0 Value           

  // Pointer to hold procedure address
D Processptr      S               *   Procptr         

D Action          S              1    Inz('A')        
D Parameter       S              5U 0                 
                                                      
 /Free                                                
  Select;     // Assign appropriate procedure address to pointer
    When Action = 'A';          
      Processptr = %Paddr(Addcust);
    When Action = 'U';          
      Processptr = %Paddr(Updcust);
    When Action = 'D';          
      Processptr = %Paddr(Delcust);
  Endsl;
                          
  Process(Parameter);     // Call appropriate procedure          
 /End-free

The prototype for each procedure must be consistent with the prototype associated with the procedure pointer; that is, they must accept the same parameters. But you can include the OPTIONS(*NOPASS) and/or OPTIONS(*OMIT) keywords to make some parameters optional or omissible.

(Portions copied from http://www.bmeyers.net/2005/09/using-callp-with-a-procedure-pointer.php)