You are on page 1of 57

RPG 400 programming

Modular Programming Constructs in RPG Overview


1
Introduction

Welcome to the module on Modular Programming Constructs in RPG 400

Moneyon Bank is halfway into migration


to RPG. Some of the developers at
Bank failed to pay attention to the
knowledge transfer provided to them by
Suavetech during the initial design of
programs. Now they are facing
maintenance nightmares. The Bank
developers must go through an
induction on structured program design
or modular programming to be able to
come out of this situation.

Click here to know why knowledge of


modular programming is important for
developers.

Why Learn Modular Programming


RPG 400 programming
Modular Programming Constructs in RPG Overview

Module Overview

This module on Modular Programming Constructs in RPG 400, has been to give you a basic understanding
of structured design.

Structured program design and Modular Programming:


1. Facilitate quality program design.
2. Helps in maintenances efficiencies.
3. Helps in the development of codes in small, independent units.
4. Facilitates development of small programs which are easier to
test than larger programs.
RPG 400 programming
Modular Programming Constructs in RPG Overview
1c
Module Objective

After completing this module on Modular Programming Constructs in RPG 400 you should be able to:

➢Explain the structured design concept.


➢Identify the relational comparisons supported by RPG.
➢Describe the different types of IF statements in RPG
and their operations.
➢Employ IF…ELSE structure.
➢Explain and use logical operators.
➢Describe and analyze Nested IF statements.
➢Define and practice SELEC statement.
➢Describe and apply DOUxx operation and DOWxx
operation that controls iterative processing.
➢Employ LEAVE and ITER operations.
➢Define and illustrate branching methodology.
➢Explain subroutine.
➢Define and Usage of CASxx structures
in subroutines.
RPG 400 programming
Modular Programming Constructs in RPG Structured Design
2
Introduction and Basic Logic Structures

Structured Design is a development methodology


Structured that has become widely accepted over the past
Basic logic
design 20 years among the developer community to
structures
facilitate quality program design.

Sequence lets you instruct the computer to execute Sequence


operations serially.

Three basic logic structures:


Also called decision, selection lets you establish
Sequence, Selection (also
alternate paths of instructions within a program;
Selection called decision) Iteration (also
which alternate the program executes depends on
called repetition or looping).
the results of a test or condition within the
program.

Also called looping, iteration permits


instructions within the program to be repeated
until a condition is met or no longer met. Iteration

Click on each of the circles to know more about


basic logic structures.
RPG 400 programming
Modular Programming Constructs in RPG Structured Design
2a
Flowchart Illustrating Basic Control Structures

Statement
False True
Condition ?
Statement

Statement Statement Statement

Condition ? Statement
Sequence
True
False Selection

Iteration
RPG 400 programming
Modular Programming Constructs in RPG Relational Comparisons
3
Relational Symbols and Codes

TABLE

Symbols Code Meaning

› GT Greater than

‹ LT Less than

RPG supports six


= EQ Equal to
relational symbols
and six two-letter
‹› NE Not equal to
relational codes
‹= LE Less than or
equal to
used with many of
the operations.
=› GE Greater than or
equal to

Relational symbols, their corresponding two letter codes,


and their meanings
RPG 400 programming
Modular Programming Constructs in RPG Exercise
4
Structured Design and Relational Comparisons

You have learned basic logic structures and relational comparison, now evaluate yourself by attending to
the exercise below. Select the correct option(s) and click Submit.

RPG uses relational comparison because:


Click on the correct answer.

• Structured operation involves comparison between the flow of control. 


•Structured operation involves testing a condition which involves comparison between values. 
•Structured program design involves comparison between variety of structured operation. 
•None of the above. 

Try Again Submit


RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations
6
Relationship

These include Uses of relational operators


in selection operation that
Relational Operators determine flow of program
EQ
LT control.
GT
LE
GE, The diagram shows relationship
NE between relational operators and
Selection Operations
selection operation.

IFxx IFGT IFGE 55


RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations
6a
Primary decision operator : IF

RPG provides structured operation IF to


implement decision logic, in program design.
The IFxx statement is used to evaluate a
condition, and depending on whether the
test is true or false, will execute the
IF statement
statements in the IF block, otherwise
transfer control to the statement outside
General format of the IF block. The relational expression
IF statement tested may be simple or complex.

Complex IF
Usage of IF statement
statement

An IFxx statement that includes more


Example Complex IF Opcode than one test connected with an AND or
OR relationship is complex.

Simple IF
statement
An IFxx statement that includes only one
test is simple.
Simple IF Opcode

Click on the blue boxes to know more


about IF statement
RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations 6a1
General format of IF statement
IF statement

Primary decision operator : IF General format of


IF statement

Usage of IF statement
Complex IF
statement

Example Complex IF Opcode

Click on the thumbnail image to view the


Simple IF
statement

Simple IF Opcode

IF statement diagram.

Factor 1 Close an IFxx Factor 2


group
Calculations to be
performed
IF xx Relational operators between IFxx statement
and its End IF
RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations
6a2
Primary decision operator : IF Example of IF statement usability
IF statement

General format of
IF statement

Click on the thumbnail image to view the


Complex IF
Usage of IF statement
statement

Example Complex IF Opcode

If statement diagram. Simple IF


statement

Simple IF Opcode

AGE IFLE (‹ =) 4
ADD 1 ChildCnt

If uses the factor 2 format of the


calculation specification. Where
the factor 2 contains the
IFxx conditional expression to be
tested.
RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations
6a3
Primary decision operator : IF Complex IF Opcode
IF statement

General format of
IF statement

Click on the thumbnail image to view the Usage of IF statement


Complex IF
statement

IF statement diagram.
Example Complex IF Opcode

Simple IF
statement

Simple IF Opcode

*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
CL0N01Factor1+++++++Opc.ode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
C
C FLDA IFEQ FLDB
C FLDA ANDGT FLDC
C FLDD OREQ FLDE
C FLDD ANDGT FLDF
C : An IFxx statement that includes more
than one test connected with an AND
C : or OR relationship is complex
C :
C ENDIF
RPG 400 programming
Modular Programming Constructs in RPG Primary decision operator : IF
6a4
Primary decision operator : IF Simple IF Opcode IF statement

General format of
IF statement

Complex IF
Usage of IF statement

Click on the thumbnail image to view the


statement

Example Complex IF Opcode

IF statement diagram. Simple IF


statement

Simple IF Opcode

Calculations to be
It includes only one
performed
Test, so the IFxx
between IFxx statement
Statement is simple
and its End IF
RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations
6b
IF…ELSE structure

The IFxx, ELSE


The syntax for the IF…. and ENDIF
IFxx statement ELSE statement Opcodes are
used to control
IFXX relational
If the evaluation in the IFxx … condition
statement is false, control ELSE processing.
passes to the ELSE statement ….
and the evaluation immediately ENDIF
following is processed.

The IFxx statement is


Else used to evaluate a
condition, and if the test is
true, the IF block operation
The relational The ELSE is processed and control
expression tested Statement provides passes to the operation
may be simple or an alternative immediately following the
complex. action if the IFxx ENDIF statement.
condition is not
true. Click on the
example button to
ENDIF look at a IF – ELSE EXAMPLE
structure program.
RPG 400 programming
Modular Programming Constructs in RPG IF…ELSE structure
6b1
Example of IF…ELSE Opcode

The IFxx statement is used to evaluate a If the evaluation in the IFxx statement is
condition, and if the test is true, the IF false, control passes to the ELSE
block operation is processed and control statement and the evaluation
passes to the operation immediately immediately following is processed.
following the ENDIF statement.
RPG 400 programming
Modular Programming Constructs in RPG QUIZ
6c
Exercise on IF and IF…ELSE structures

Drag the boxes containing answers and drop them at the correct blank spaces on the green screen:

You are asked to calculate pay and wanted to pay time-and-a-half for any hours over 40, you could code.

*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
Given Value
CL0N01Factor1+++++++Opc.ode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
C HOURS IF EQ
1 40 A=HOURS-40
C HOURS MULT PAYRATE TOTALPAY B=PAYRATE*1.5
C 5
ELSE C=PAYRATE(A)
C 2
HOURS SUB 40 A
D=C*B
C 3
PAYRATE MULT 1.5 B TOTALPAY = 40*D
C 4
PAYRATE MULT A C

C C MULT B D

C 40 MULT D TOTALPAY Try Again

IFEQ IFGT 2 PAYRATE 3, 4


Submit
1 ELSE
IFNE HOURS
5
RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations
6d
Logical Operators

Logical operators
Programmers sometimes want are used to
to execute a series of When an AND relationship combine the
instructions based on multiple is included, all the conditions, when
conditions. RPG includes logical conditions should be true to several conditions
operators to allow such multiple execute the action need to be
conditions. checked.

ANDxx Opcodes
Logical operators

Relational Statements in RPG can


be combined using the ANDxx
and ORxx Opcodes

ANDxx and ORxx are combined to


ORxx Opcodes create more complex conditional
tests.

When an OR relationship is included, any one of the conditions


should be true to execute the action EXAMPLE
RPG 400 programming
Modular Programming Constructs in RPG Logical Operators
6d1
Example: Logical Operators

ANDxx are combined with other


operations to form extremely complicated
conditional tests.
RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations
6e
Nested IF Statements

IF the sales level is less than or equal to 5000


ADD 0.0075 commission RATES
ENDIF
A pseudo code on the
ELSE blackboard illustrates a
IF the sales level is less than or equal to 10000 nested IFxx, the code
expresses that same
ADD 0.0075 commission RATES logic in RPG. If there is a
ENDIF large number of
nestings, the code could
ELSE become very
IF the sales level is less than or equal to 15000 complicated and hard to
understand. For
ADD 0.0075 commission RATES example, for each and
ENDIF every repeated operation
there is an ENDIF
ELSE Opcode.
ADD 0.1 Rates
ENDIF
SAMPLE
RPG 400 programming
Modular Programming Constructs in RPG Nested IF Statements
6e1
Sample of Nested IF Opcode
RPG 400 programming
Modular Programming Constructs in RPG QUIZ
6f
Exercise on Logical operators and Nested IF

Drag the boxes containing answers and drop them at the correct blank spaces on the green screen:

*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
Submit
CL0N01Factor1+++++++Opc.ode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
C* If ACODE is equal to A and indicator 50 is on, the MOVE ELSE
C* and WRITE operations are processed.
C ACODE IFEQ 1'A' ANDEQ*ON
C *IN50 2
ANDEQ*ON
C MOVE 'A' ACREC
C WRITERCRSN
ANDEQ*OFF
C* If the previous conditions were not met but ACODE is equal
C* to A, indicator 50 is off, and ACREC is equal to D, the
C* following MOVE operation is processed.
ANDEQ'D'
C 3
ELSE
C ACODE IFEQ 'A'
C *IN50 4
ANDEQ*OFF
C ACREC 5
ANDEQ'D' IFEQ 'A'
C MOVE 'A' ACREC
C 6
ENDIF
Try Again
C ENDIF
RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations
6g
Case Logic

The following example of assigning commission RATES typifies program construct,


sometimes called CASE logic.

To simplify coding of CASE SELEC


SELECTION OPERATION logic RPG has these two
selection operation CASxx
BLINK 1
RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations
6h
Select (SELEC) Operation

Select operation (SELEC) A SELEC Group conditionally processes a


sequence of one or more relational WHxx
(When True then SELECT) statements.

A SELEC Syntax for the SELEC opcode


statement
(required) SELEC
WHXX
When statement (WHxx) Zero or more ------
WHxx statements
(optional) WHXX
-------
OTHER operation OTHER
and statement
Other Operation (OTHER) -------
group (optional)
ENDSL
An ENDSL (End
Select) statement
(required)
End Select Operation (ENDSL)
RPG 400 programming
Modular Programming Constructs in RPG Select (SELEC) Operation
6h1
SELEC Opcode

Within a SELEC group, control passes to After the instructions for the
One or more
the first instruction following the WHxx WHxx statement are executed
WHxx statements
statement tested as true. control branches to the
instruction following the ENDSL
SELEC group statement.

WHxx statements

SELEC Statement OTHER statement ENDSL statement


WHxx statements

After the instructions for


the OTHER statement are
WHxx tests If none of the WHxx statements within executed control branches
are not true the SELEC group test as true, to the instruction following
instructions following the OTHER the ENDSL statement.
operation are automatically executed.

If OTHER operation is also not specified, control branches to the


instruction following the ENDSL statement
RPG 400 programming
Modular Programming Constructs in RPG Select (SELEC) Operation
6h2
SELEC Opcode : Example

OTHER statement
Factor 1, Factor 2 and Result
WHxx statements field are not used with the
SELEC statement
RPG 400 programming
Modular Programming Constructs in RPG Nested IFxx vs. SELEC operations
6h3
Comparison

This Opcode demonstrates Which


the use of a nested IF operation,
statement Nested
IFxx/SELEC
would you as a
programmer
choose.

This piece of code does the


same thing as the previous
code, using a SELEC
statement

Click on the screen shot to zoom out to display a larger area of the image.
RPG 400 programming
Modular Programming Constructs in RPG Exercise
7
Opcode on case logic and SELEC operations

Drag the boxes containing answers and drop them at the correct blank spaces on the green screen:
*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
CL0N01Factor1+++++++Opc.ode(E)Factor2+++ResultLenDHHiLoEqComments++++++ You have previously
C* seen codes using
Nested IF. Here, the
C 1
SELEC code on the left side
C X WHEQ uses SELEC to
2 1
express that if X
C Z-ADDA B equals 1, do the
C MOVE C D operations in
sequence 1; if X
C : seq 1 does not equal 1,
C Y 3 2
WHEQ and if Y=2 and
X<10, do the
C X ANDLT10 operations in
C : seq 2 sequence 2.

C OTHER
4
C : seq 3
Submit
C ENDSL
C*

Try again
OTHER WHEQ 2 ANDEQ*ON WHEQ 1 WHEQ 3
RPG 400 programming
Modular Programming Constructs in RPG Looping Operations
7
Operations for Iteration

Iteration lets your


program repeat a series
of instructions, a
common necessity in
DOWxx (Do while)
programming.
operation

Iteration Operations

DOUxx (Do Until)


DO (Do) operation
Operation

The DOUxx, DOUxx, and DOxx operation is a structured instruction that controls
iterative processing.
RPG 400 programming
Modular Programming Constructs in RPG Operations for Iteration
7a
DOWxx (Do while) operation
A programmer uses
The statements between the DOW and DOW (DO…WHILE)
ENDDO would be repeated as long as for different kind of
the condition specified in the DOW repeat processing
statement is true. When the condition operation.
becomes false, control is transferred to DOWxx
the statement following the ENDDO
statement. <….>
ENDDO

DO…WHILE Structure

DOWxx
Like RPG decision
operations, the
DOW operation
<….>
lets a
programmer use xx operation tests that may be
AND or OR to specified are LT, GT, LE, GE, EQ or
form compound ENDDO NE.
condition to
control the
looping. Example
RPG 400 programming
Modular Programming Constructs in RPG DOWxx (Do while) operation
7a1
DOWxx operation: Example

Use of DOWxx Loop


RPG 400 programming
Modular Programming Constructs in RPG Operations for Iteration
7b
DOUxx (Do Until) operation
DOU is a structured
The statements between the DOU and iteration very similar
ENDDO would be repeated until the to DOW.
condition specified in the DOU
statement becomes true. When the
condition becomes true, control is DOUxx
transferred to the statement following <….>
the ENDDO statement. ENDDO

DO…UNTIL Structure
DOUxx
Like RPG decision
operations, the
DOU operation
<….>
lets a
programmer use xx operation tests that may be
AND or OR to specified are LT, GT, LE, GE, EQ or
form compound ENDDO NE.
condition to
control the
looping. Example
RPG 400 programming
Modular Programming Constructs in RPG DOUxx (Do Until) operation
7b
DOUxx operation: Example

Use of DOUxx Loop


RPG 400 programming
Modular Programming Constructs in RPG Looping Operations
7c
DOWxx operation vs. DOUxx (Do Until) operation

DOU is a trailing decision loop, as the


Dow repeats while the comparison is made after the instructions within
specific condition remain Leading-Decision Loop: the loop have been executed, the instructions
true. will always execute at least once.
Do While

Statement (s)
Condition ? Statement (s)
True
False
False
Condition ?

True

Dow is a leading decision loop, which means the


comparison is made before the instructions within
the loop are executed for the first time. If the
DOU operation repeats
comparison evaluates to false, the computer Trailing-Decision Loop:
until the condition
completely bypasses the instruction within the
becomes true. Do Until
loop.
RPG 400 programming
Modular Programming Constructs in RPG Looping Operations
7d
DOxx (Do) operation

The starting value of The limiting value of the A field to RPG gives the programmer the
the counter, the counter, for looping to serve as the option to specify the four values
default value is one.
continue. counter or omit any of them.

For
programmers
General layout of a DO loop DOxx operation
are designed for
count-controlled
CLONO1Factor1++++Opcode (E)+Factor2++++++Result loop.

Start value DOxx Limited value Counter


……………
ENDDO Increment value

It controls the processing of a group of C-Spec The amount to be added to


statements in the DO Group a predetermined the counter at the end of each
number of times. repetition of the loop. Example
RPG 400 programming
Modular Programming Constructs in RPG DOxx (Do) operation
7d1
DOxx operation: Example

Limit value = The limiting value of


DO Loop for ABC to be
The initial the counter for looping to
displayed (DSLP) on the
value (start continue, i.e., 123 times.
screen 123 times.
value) is
assumed a
default value
of one.
RPG 400 programming
Modular Programming Constructs in RPG Exercise
7e
Opcode on DOWxx

Write parts of a programs to generate the operation provided below. Self check your program by clicking
the answer button. The operation given repeat a series of instructions in a program.

Employees in your Bank wants you to write a program where they would be able to add
all the numbers between 1 and 100. with a counter field, Number, and an accumulator,
Sum. Use DOWxx Opcode to accomplish the summation.

*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
CL0N01Factor1+++++++Opc.ode(E)Factor2+++ResultLenDHHiLoEqComments++++++
C
C
C
C
C
C
C
C
C
C
C
C Answer
RPG 400 programming
Modular Programming Constructs in RPG Looping Operations
8
Loops and Early Exits

It helps
programmers skip
LEAVE operation ITER operation
the remaining
instructions with a
loop or exit a
complete loop.
When a LEAVE The ITER statement passes
statement is control to the related ENDDO
executed, control is statement and causes the next
transferred to the iteration of the loop to be
statement following executed.
the related ENDDO
statement.
ITER = skip
the remaining Instructions
instructions in with the loop
The LEAVE (Leave a
the loop
DO Group) and ITER
(Iterate) operations
support additional
control within a DO, LEAVE = leave
DOUxx or DOWxx the looping
group. process Example
completely
RPG 400 programming
Modular Programming Constructs in RPG Looping Operations
8a
Opcode on LEAVE And ITER
RPG 400 programming
Modular Programming Constructs in RPG Exercise
8b
Opcode on LEAVE and ITER

Drag the blue boxes containing answers and drop them at the correct blank spaces on the green screen:
*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
CL0N01Factor1+++++++Opc.ode(E)+Factor2+++ResultLenDHHiLoEqComments++++++ Explanation
C
Here, the Opcode uses a
C DOU
1 FLDA = FLDB ENDDO DOU loop containing a
DOW loop. The IF
C :
statement checks indicator
C NUM DOWLT
2 10 ITER 01. If indicator 01 is ON, a
certain operation is
C IF *IN01
executed, transferring
C LEAVE
3 IFEQ control out of the
innermost DOW loop to the
C ENDIF
4 Z-ADD instruction. If
C EXSR PROC1 DOWLT indicator 01 is not ON,
subroutine PROC1 is
C *IN12 IFEQ
5 *OFF
processed. Then indicator
C ITER
6 DOU 12 is checked. If it is OFF,
operation transfers control
C ENDIF
to the innermost ENDDO
C EXSR PROC2 ENDIF and the condition on the
DOW is evaluated again. If
C ENDDO
7 indicator 12 is ON, another
C Z-ADD 20 RSLT 20 operation PROC2 is
LEAVE processed.
C :
C ENDDO
C : Submit
Try again
RPG 400 programming
Modular Programming Constructs in RPG Branching Operations
9
Branching Operation: Introduction

Both the operations are unstructured because neither


has build-in constraints to limit how it directs flow of
control.

Branching
Operation

Traditional Syntax Traditional Syntax


Compare
CABxx and Go To GOTO
Branch

Traditional Syntax
Tag
TAG

The TAG operation names the label that identifies the


destination of a GOTO or CABxx operation.
RPG 400 programming
Modular Programming Constructs in RPG Branching Operations
9a
Unconditional GOTO statement

For a
The GOTO statement transfers control to the line of code with
programmer
the corresponding TAG name specified
the GOTO
operation
diverts control
*...1.....+.....2.....+.....3.....+.....4.....+.....5.....+....6....+....7...+.... to a different
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq location within
a program.
C GOTO Label
C .......
C TAG
C ...... A non-
executable
C GOTO END
operation
C END TAG

General layout of a GOTO Opcode

The TAG can either precede or follow the GOTO.


Example
RPG 400 programming
Modular Programming Constructs in RPG Unconditional GOTO statement
9a1
GOTO Opcode: Example

The GOTO used to set up


unconditional transfer of control.
RPG 400 programming
Modular Programming Constructs in RPG Branching Operations
9b
Unstructured – Compare and Branch statement

When branching is to be done One of the relational tests


When branching is to be done
based on a condition, a CABxx/ (EQ, GT, LT, GE, LE, NE) is
based on a condition, a CABxx/
TAG statement can be used. TAG statement can be used. substituted for xx

Factor 2 must
contain a field
*...1.....+.....2.....+.....3.....+.....4.....+.....5.....+....6....+....7...+.... name or literal,
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq the same type as
C A CABxx B SKIP Factor 1.

C .......
A TAG operation
C SKIP TAG name must be in
A TAG operation names the
C ...... destination of a CABxx the Result field
C LOOP TAG operation. The TAG can either
precede or follow the CABxx.

General sample of a CABxx Opcode

Factor 1 must contain a If the condition returns true, control will be transferred
field name or literal. to the TAG statement mentioned on that line of code. Example
RPG 400 programming
Modular Programming Constructs in RPG Unstructured – Compare and Branch statement

CABxx Opcode: Example


9b1

The CABxx operation is simply a


GOTO in disguise.
RPG 400 programming
Modular Programming Constructs in RPG Exercise
9c
Opcode on GOTO and CABxx

Fill in the blanks with appropriate phrases. Write your answers in the blanks provided.

A) Use a GOTO operation to specify a branch:


1. 1
From a detail calculation line to another detail calculation line.
2. 2
From a total calculation line to another total calculation line.
3. 3
From a detail calculation line to a total calculation line.
4. From a subroutine to a TAG or 4ENDSR within the same subroutine.
5. 5
From a subroutine to a detail calculation line or to a total calculation line.

B) A CABxx operation in a sub procedure can specify a branch:

1. 6 line in the body of the sub procedure.


From a line in the body of the sub procedure to another
2. 7
From a line in a subroutine to another line in the same subroutine.
3. From a line in a subroutine
8 to a line in the body of the sub procedure.

Try again
Submit
RPG 400 programming
Modular Programming Constructs in RPG Subroutines
10
Defining Subroutines

As programs become complex, For a


The size and number of internal programmer
subroutines specified will depend it is necessary to break them
up into smaller components one vehicle for
on program logic, complexity and top-down,
called subroutines.
the interpretation of the modular
programmer. program design
in RPG is the
subroutine.
Logically related
processing is
done in one Complex program
subroutine.

Opcodes used for Complex program are broken down by


program separating the calculations into internal
Subroutines subroutines
subroutines

Example
BEGSR ENDSR EXSR

Control is sent to the named


Identifies the beginning of Signals the endpoint of the subroutine and is returned to the next
a subroutine. block of code. line after the subroutine is executed.
RPG 400 programming
Modular Programming Constructs in RPG Subroutines
10a
BEGSR Opcode: Example

BEGSR includes
the name of the
subroutine SINIT
RPG 400 programming
Modular Programming Constructs in RPG Subroutines
10b
EXSR Opcode: Example

S
U
B
R
O
U
T
I
N
E
S
RPG 400 programming
Modular Programming Constructs in RPG Subroutines
10c
INZSR Subroutine

*INZSR is the initialization subroutine used to initialize variables at the beginning of the program.

*...1.....+.....2.....+.....3.....+.....4.....+.....5.....+....6....+....7...+....
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq
C *INZSR BEGSR
C -------
If you include INZSR within your
C ------- program, the subroutine is invoked
automatically after the program
C -------
completes its other tasks.
C ENDSR

Sample code initializing variables using the built-in subroutine *INZSR


General format of a INZSRxx Opcode

This Subroutine is defined by specifying *INZSR in Factor 1 of the BEGSR operation in a C-Spec operation

Click on the button to view a sample *INZSR Opcode with a program Sample
RPG 400 programming
Modular Programming Constructs in RPG INZSR Subroutine
10c1
INZSR Subroutine: Sample
RPG 400 programming
Modular Programming Constructs in RPG Subroutines
10d
CASxx Operation

The CASxx opcode should be used, when a relational test CASxx also lets a
is required before exiting to an internal subroutine. programmer
specify a
subroutine to be
General layout of a CASxx Opcode
performed if none
*...1.....+......2......+......3......+.....4......+......5......+.....6.....+.....7...+.... of the tested
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq
condition prove
to be true. For
C FieldA CASGE FieldB Subr01 this option a
C FieldA CASEQ FieldC Subr02 CASxx is used
A required ENDCS without a
C CAS Subr03 indicates the end of a relational code,
single CASxx as the final
C ENDCS statement or group. operation within
the structure.

Both CAS and CAB statement branch to different parts of the program.

CASxx vs. CABxx


CAS will branch to a CAB will branch to a line tagged
subroutine. with the TAG statement.

Example
RPG 400 programming
Modular Programming Constructs in RPG Subroutines
10d1
CASxx Opcode: Example

A CAS group can contain only


An ENDCS operation must follow
CASxx operations.
the last CASxx operation to
denote the end of the CAS
group.
RPG 400 programming
Modular Programming Constructs in RPG Self-Evaluation
11
Programming Assignment

Your banking company wants you to write a payroll program. Each record in the input file,
MPR001 represents one day’s work for an employee. Records are accumulated doe a week,
Scenario
so there will be several records per employee. Records in the file are ordered by the Social
Security number.

The company uses both an hourly rate and a piece rate to pay its employees. Everyone
gets Rs. 50 per hour worked. Additionally is a person works for one hour extra during the
Addi week, (s) he receives Rs. 50 per hour; if (s) he works for 3 hours extra, (s) he receives Rs.
tion 100 per hour; if over 5hrs extra, he receives Rs. 500 per hour. After using these figures to
al calculate gross pay, a 4.6 percent profession tax and a 15 percent income tax must be
Inpu subtracted from the gross pay to obtain net pay.
t

Your program is to generate the report illustrated in the chart provided.

Click next to view the MPR001: Work file and the desired report after the
program is run.
RPG 400 programming
Modular Programming Constructs in RPG Self-Evaluation
11a
Supplied Material: I

MPR001: Moneyon Bank Work File


Input record format for MPR001:
Scenario
Field Description Positions (Decimal
position)
Additional Input
SOCSEC Social security 1-9 (0)
Number
Programming
assignment NAME Name 10-25 (0)
WKDATE Date worked 26-31 (0) MMDDYY
HOURS Hours worked 32-33 (0)
EXTA HOURS Extra hours 34-36 (0)
worked

Click next to view desired report after the program is run.


RPG 400 programming
Modular Programming Constructs in RPG Self-Evaluation
11b
Supplied Material: I

XX/XX/XX MONEYON BANK PAYROLL REPORT Page xx0x

HOURS EXTRA GROSS PROFESSI INCOME NET


ON
SOC. SEC. NAME WORKED HOURS PAY TAX TAX PAY

XXX-XX- XXX XXXXXXX 0X X,XOX X.XX0.XX XO.XX XX0.XX X.XX0.XX

XXX-XX- XXX XXXXXXX 0X X,XOX X.XX0.XX X0.XX XX0.XX X.XX0.XX

XXX-XX- XXX XXXXXXX 0X X,XOX X.XX0.XX XO.XX XX0.XX X.XX0.XX

AVERAGE GROSS PAY: Rs. XX0.XX

AVERAGE HOURS PER EMPLOYEE: X.XOX

AVERAGE EXTRA PER DAY: X.XOX


HOURS

Programming
Scenario Additional Input
assignment

Click next to view the answer sheet.


RPG 400 programming
Modular Programming Constructs in RPG Self-Evaluation
11c
Answer Sheet

*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
CL0N01Factor1+++++++Opc.ode(E)+Factor2+++ResultLenDHHiLoEqComments++++++
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
Programming
Scenario Additional Input SUBMIT ANSWER
assignment
RPG 400 programming
Modular Programming Constructs in RPG End of the lesson
12
Summary

After completing this lesson on Modular Programming Constructs in RPG/400 have learned the
following leaning points and should be able to apply them in program design. This would help in
better maintenance and qualify of the program designed for the organization you work with.

 The structured design and the three basic logic structures.


 Relational comparisons supported by RPG.
 The different types of IF statements in RPG
 Logical operators.
 Nested IF statements.
 SELEC statement.
 DOUxx operation, DOWxx and DOxx
 Iterative processing.
 LEAVE and ITER operations.
 Branching methodology.
 Subroutine.
 CASxx structures in subroutines.

You might also like