Using Subroutines [ HP RPG/XL Programmer's Guide ] MPE/iX 5.0 Documentation
HP RPG/XL Programmer's Guide
Using Subroutines
When there are a set of operations that you perform repeatedly from
different parts of your program, you should code them once as a
subroutine. Then, when you want to execute the subroutine, you enter a
Calculation Specification to perform it.
RPG has two kinds of subroutines. Internal subroutines are part of an
RPG program. External subroutines are independent load modules that are
written in Business BASIC, C, Pascal, SPL or COBOL.
Internal Subroutines
To perform an internal subroutine, enter an EXSR Calculation
Specification operation. Place the subroutine, itself, after the last
Calculation Specification in the program. The BEGSR operation marks the
beginning of an internal subroutine and always has a tag associated with
it. You use this tag to execute the subroutine. To end an internal
subroutine, follow the last line in the subroutine with an ENDSR
operation.
Figure 5-14 shows how internal subroutines are coded. In this example,
the subroutine is named, INQRY.
Figure 5-14. Using an Internal Subroutine
Comments
1 This line performs the internal subroutine INQRY when a user
presses function key F3. (Conditioning the EXSR operation is
optional.)
Columns 28-32 contain EXSR to execute an internal subroutine.
Columns 33-38 contain the name of the internal subroutine,
INQRY.
2 This line begins the INQRY internal subroutine.
Columns 7-8 contain SR to indicate that this is a subroutine
line (SR is optional on this and subsequent subroutine lines).
Columns 18-23 contain the name of the internal subroutine INQRY.
Columns 28-32 contain BEGSR to mark the beginning of the
internal subroutine.
3 This line transfers control to the end of the internal
subroutine. You cannot use GOTO operations to transfer control
out of the subroutine.
4 This line ends the internal subroutine.
Columns 18-23 contain the tag, INQRY9. (A tag is used in this
particular example, though it is optional.)
Columns 28-32 contain ENDSR to end the internal subroutine.
External Subroutines
External subroutines are separate procedures; you do not code them as
part of an RPG program. For example, you can code an external subroutine
in Business BASIC. You then compile it, and place it in an executable
library using HP Link Editor/XL. To execute the external subroutine from
an RPG program, enter an EXIT Calculation Specification operation. The
HP RPG Reference Manual contains information on how to create external
subroutines in COBOL and other languages.
There are two ways to pass information to external subroutines. The
first method uses the Calculation Specification operation, RLABL, to name
the fields, arrays, tables or indicators that you want to pass. The
second method uses the Calculation Specification operation, PARM, to name
the fields, arrays and tables to pass. PARM is more limited than RLABL
because you cannot pass indicators. Also, PARM values are available only
to the subroutine executed with the PARM operation(s). The next two
sections explain how to use external subroutines and how to pass
parameters to them using RLABL and PARM.
Using RLABL. RLABL passes field names, tables, arrays and indicators to
external subroutines. Values passed by RLABL are available to all
external subroutines in the program.
Figure 5-15 shows a portion of a C procedure that is used as an external
subroutine. Just the statements defining the data that is passed to the
procedure (the field, PNAME, and the indicator, IN20) are shown. Figure
5-16 shows how to execute this C procedure from an RPG program.
_____________________________________
| |
| VOID SUB01() |
| { |
| EXTERN CHAR PNAME[]; |
| EXTERN INT IN20; |
| . |
| . |
| . |
| } |
| |
_____________________________________
Figure 5-15. An External Subroutine written in C
Figure 5-16 lists the Specifications that execute the external
subroutine, SUB01, shown in the previous figure. The RPG program passes
indicator 20 (IN20) and the field (PNAME) to the subroutine.
Figure 5-16. Using RLABL to Pass Information to an External Subroutine
Comments
1 This line makes indicator 20 available to an external
subroutine.
Columns 43-48 specifies that indicator 20 is passed to the
external subroutine. (Prefix indicator names by IN.)
2 This line makes the field, PNAME, available to an external
subroutine.
3 This line executes the external subroutine, SUB01.
Using PARM. PARM passes field names, tables and arrays to external
subroutines. PARM does not pass indicators. Also, you must code PARM
operations for each external subroutine called. PARM values are not
globally available to other external subroutines in a program. (See
Figure 8-4 for a complete program that uses external subroutines and
PARM.)
RPG variables used with PARM are passed to external subroutines as byte
values by reference. The corresponding external subroutine variables
must be declared accordingly. (All RPG numeric fields have packed
decimal formats.)
Figure 5-17 shows part of a C procedure that is executed as an external
subroutine. Only the statements defining the data that is passed to the
procedure are shown. Figure 5-18 shows how this C procedure is executed
from an RPG program.
_____________________________________
| |
| VOID SUB01 (PNAME, STATUS) |
| CHAR *PNAME; |
| CHAR *STATUS; |
| { |
| . |
| . |
| . |
| } |
| |
_____________________________________
Figure 5-17. An External Subroutine Written in C
Figure 5-18 shows how to execute the C procedure shown in Figure 5-16.
The RPG program passes two fields to the C procedure, PNAME and STATUS.
The C procedure may alter the value in STATUS, indicating that an error
occurred in the external subroutine.
Figure 5-18. Using PARM to Pass Information to an External Subroutine
Comments
1 This line moves a program name, RPT10, to the first parameter
field, PNAME. This field is passed to the external subroutine.
2 This line sets the field, STATUS, to zeros. This field is
passed to the external subroutine. The subroutine returns an
error code in this field.
3 This line executes the external subroutine, SUB01. SUB01 is the
C procedure shown in Figure 5-17.
4 This line passes the field, PNAME, to the external subroutine.
(You enter PARM operations immediately after the EXIT
operation.)
5 This line passes the field, STATUS, to the external subroutine.
6 This line tests the value in STATUS. The external subroutine can
modify this field.
MPE/iX 5.0 Documentation