CALLING A PROCEDURE


The syntax for evoking a procedure or subprocedure is the same as that of a built-in function. Procedures are sometimes referred to as user-written functions. In this case, the user is the RPG programmer.

There are two basic techniques used to evoke (i.e., CALL) subprocedures. The first type is as the value of the extended factor 2 entry of the CALLP operation. This operation accepts subprocedure names that have been prototyped. The parameters of the subprocedure are specified similarly to that of a built-in function. See Figure 10.3.

Figure 10.3: Calling a subprocedure with CALLP.

start example
 .....CSRn01..............OpCode(ex)Extended-factor2+++++++++++++++++++++++++++++      C                   CallP     myFunct( p1 : p2 : p3 : p4) 
end example

As shown in Figure 10.3, the CALLP operation accepts the name of a prototype that identifies a subprocedure to call in factor 2. If parameters can be required, they are specified on the same line as the CALLP operation and enclosed in parentheses.

The second type of subprocedure call is within an expression. This can be a conditional expression (such as the IF, DOW, DOU, and WHEN operations), in an assignment expression on the EVAL operation, or as the return value for the RETURN operation. Figure 10.4 shows three examples of how to evoke a subprocedure from within an expression.

Figure 10.4: Calling a subprocedure within an expression.

start example
 .....CSRn01..............OpCode(ex)Extended-factor2+++++++++++++++++++++++++++++ 0001 C                   IF         myFunct( p1 ) = myFunct2( p2 ) 0002 C                   DOW        myFunct3( p3 * 50 ) < 10000 0003 C                   EVAL       var = myFunct4( p4 ) 0004 C                   RETURN     myFunct5( p5 ) + 7 
end example

In Figure 10.4, the subprocedures MYFUNCT and MYFUNCT2 (line 1) are used in a conditional expression. On line 2, –MYFUNCT3 is used to condition a DOW loop by comparing its return value to the literal 10000. The parameter of the MYFUNCT3 subprocedure also is an expression. On line 3, the subprocedure MYFUNCT4 is used to assign its return value to the VAR field on line 4; MYFUNCT5 is used to calculate the value returned to the calling procedure.

When a subprocedure is evoked through a conditional or assignment operation, a return value is expected. When a subprocedure is evoked using the CALLP operation, a return value is optional and is ignored by the operation. Normally, the CALLP operation is used to call a subprocedure that has no return value or to evoke a stand-alone program that has been prototyped.

In addition to subprocedures, stand-alone programs can be prototyped. This allows a program to be called through the CALLP operation, and to have strong parameter-type checking performed by the compiler.

In Figure 10.5, two prototypes are specified. The first (lines 1 and 2) is for a subprocedure named TOUPPER. The second (lines 3 to 6) is for a stand-alone program named QCMDEXC. The name used to call QCMDEXC in this program is RUN. The EXTPGM keyword (line 3) names the external program to be evoked and the prototype name (positions 7 to 21 of line 3) is used in the program.

click to expand
Figure 10.5: Procedure and program prototypes.

The compiler ignores the names specified for the parameters of a prototype (lines 4 to 6). In Figure 10.5, line 4 specifies the name CMDSTR as the first parameter of the RUN prototype. The stand-alone field (line 7) is also named CMDSTR. The compiler throws out the name on line 4; therefore, it can be reused on line 7.




The Modern RPG IV Language
The Modern RPG IV Language
ISBN: 1583470646
EAN: 2147483647
Year: 2003
Pages: 156
Authors: Robert Cozzi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net