Вы находитесь на странице: 1из 16

NDVO

NDVOR IT Solutions (P) Ltd.,


IT Services Redefined

Subroutines

NDVO
R

AS/400 Training

Subroutines

NDVO
R

Subroutines

Subroutine is one of the major constructs RPG IV supports to deal with top down design
and modular development.
A subroutine is a block of code inside a program, with an identifiable beginning and end.
Subroutine is coded between Begsr (Begin subroutine) and Endsr (End subroutine)
operations.
After performing the subroutine, the program returns control to the statement immediately
following the one that invoked the routine.
Each subroutine must have a unique symbolic name.

04/13/15

Showme Consultants

NDVO
R

Subroutines

The following code shows the skeleton of a subroutine,


In free RPG format,

In RPG ILE format,

04/13/15

Showme Consultants

Execute Subroutines (Exsr)

NDVO
R

To send control to a subroutine for execution, we use the


EXSR operation.
The Exsr operation causes the user subroutine named in the
subroutine-name operand to be processed.
When the computer encounters the Exsr operation, control
drops to the subroutine.
Upon completion of the routine, control returns to the
operation immediately following the calculation that invoked
the subroutine.

04/13/15

Showme Consultants

Executing Subroutines

NDVO
R

The following code shows the skeleton of a Exsr operation,


In free RPG format and RPG ILE format,

04/13/15

Showme Consultants

Leave a Subroutine (Leavesr)

NDVO
R

The LEAVESR operation exits a subroutine from any point


within the subroutine.
The control passes to the ENDSR operation for the subroutine.
Leavesr is allowed only from within the subroutine.
Example;

04/13/15

Showme Consultants

Conditionally Invoke subroutine(CASxx)

NDVO
R

Syntax:

The CASxx operation is used to conditionally select a


subroutine for processing. The selection is based on the
relationship between factor 1 and factor 2, as specified by xx.
A CAS group can contain only CASxx operations. An ENDCS
operation must follow the last CASxx operation.

04/13/15

Showme Consultants

Conditionally Invoke subroutine(CASxx)

NDVO
R

Examples;

04/13/15

Showme Consultants

Conditionally Invoke subroutine(CASxx)

NDVO
R

The result field of the CASxx operation must contain the name
of a user subroutine or one of the following the keywords:
*TERMSR, *PSSR or *INZSR;
1. *TERMSR specifies a subroutine to be run during
normal termination.
2. *PSSR specifies that this is a program exception/error
subroutine to handle program-detected exception/errors.
3. *INZSR specifies a subroutine to be run during
initialization.

04/13/15

Showme Consultants

10

NDVO
R
*TERMSR & *PSSR

*TERMSR(Normal termination Subroutine): When normal


termination occurs, subroutine *TERMSR is called.
*TERMSR is a user written subroutine from which any final
code execution can occur. At the time that *TERMSR is
invoked, no action subroutines are active, and the current
component has been marked as being in termination.
*PSSR(Program Exception Subroutine): To identify the userwritten subroutine that is to receive control when a program
exception/error occurs, specify *PSSR in factor 1(sub routine
name) of the Begsr operation.

04/13/15

Showme Consultants

11

NDVO
R
*INZSR (Initialization subroutine)

The initialization subroutine allows you to process calculation


specifications. It is declared like any other subroutine, but with
*INZSR in factor1(sub routine name).
Only one sub routine can be defined as an initialization
subroutine.

04/13/15

Showme Consultants

12

Acknowledgements

NDVO
R

We would like to acknowledge the IBM website, Red books


and other reference and user guides.
We also recommend the students read the following and use
them as reference
Infocentre
C0918160.pdf - RPG400 Users Guide
C0918170.pdf - RPG400 Reference
RZKH free AS400.

04/13/15

Showme Consultants

13

Trademarks and notices

NDVO
R

The following are trademarks of the IBM Corporation in the United


States or other countries:
Application System/400 | AS/400
400
ILE
IBM
OS/2 | Operating System/2
OS/400 | Operating System/400
RPG/400
SAA | Systems Application Architecture
SQL/400
All other trademarks are of respective owners
04/13/15

Showme Consultants

14

Copyrights and other notices

NDVO
R

Ndvor IT Solutions.
The following notices applies to the set of training material on
IBM AS/400 training topics
The recipient of this training material can print maximum of
10 copies of the training material or the number authorized by
the copyright holder, whilst distributing it.
Electronic distribution is restricted to the participants of the
classroom training. Who are in turn obligated to respect the
copyright and refrain from distributing it.
Any inquires, permissions in this regard, please use the
Contact us form on the website.

04/13/15

Showme Consultants

15

NDVO
R

04/13/15

Showme Consultants

16

Вам также может понравиться