RPG/400 Help

Coding Subroutines

An RPG/400 subroutine can be processed from any point in the calculation operations. All RPG/400 operations can be processed within a subroutine, and these operations can be conditioned by any valid indicators in positions 9 through 17. SR or blanks can appear in positions 7 and 8. Control level indicators (L1 through L9) cannot be used in these positions. However, AND/OR lines within the subroutine can be indicated in positions 7 and 8.

Fields used in a subroutine can be defined either in the subroutine or in the rest of the program. In either instance, the fields can be used by both the main program and the subroutine.

You can include a maximum of 254 subroutines in a program; however, a subroutine cannot contain another subroutine. One subroutine can call another subroutine; that is, a subroutine can contain an EXSR or CASxx. However, an EXSR or CASxx specification within a subroutine cannot directly call itself. Indirect calls to itself through another subroutine should not be performed, because unpredictable results can occur. Use the GOTO and TAG operation codes if you want to branch to another point within the same subroutine.

Subroutines do not have to be specified in the order they are used. Each subroutine must have a unique symbolic name and must contain a BEGSR and an ENDSR statement.

The use of the GOTO (branching) operation is allowed within a subroutine. GOTO can specify the label on the ENDSR operation associated with that subroutine; it cannot specify the name of a BEGSR operation. A GOTO outside the subroutine cannot be issued to a BEGSR, ENDSR, or TAG within a subroutine. A GOTO within a subroutine can be issued to a TAG within either detail or total calculations.

See Figure "Example of Coding Subroutines" for an example of coding a subroutine.

Subroutine Coding Examples

Figure 29. Example of Coding Subroutines

*...1....+....2....+....3....+....4....+....5....+....6....+....7...
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++
C*
C* For a subroutine, positions 7 and 8 can be blank or contain SR.
C*
C                         :
C                         :
C                     EXSR SUBRTB
C                         :
C                         :      CALCULATIONS
C                         :
CL2                   EXSR SUBRTA
C                         :
C                         :      CALCULATIONS
C                         :
C           SUBRTA    BEGSR
C                         :
C                         :      CALCULATIONS
C                         :
C*
C*  One subroutine can call another subroutine.
C*
C                     EXSR SUBRTC
C                         :
C                         :      CALCULATIONS
C                         :
C                     ENDSR
C           SUBRTB    BEGSR
C                         :      CALCULATIONS
C                         :
C                         :
C*
*...1....+....2....+....3....+....4....+....5....+....6....+....7...
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++
C*
C*  GOTO and TAG operations can be used within a subroutine.
C*
C           START     TAG
C                         :
C                         :      CALCULATIONS
C                         :
C   23                GOTO END
C                         :
C                         :      CALCULATIONS
C                         :
C   24                GOTO START
C           END       ENDSR
C           SUBRTC    BEGSR
C                         :
C                         :      CALCULATIONS
C                         :
C                     ENDSR
C*


[ Top of Page | Previous Page | Next Page | Table of Contents ]