BAC - Training Programme
BAC - Training Programme
RPG/400
RPG/400 - Agenda for Day 1
• Introduction • F Specifications
• C specifications
• Brief History
• Data types
• Structure of RPG • Operation codes
• Symbolic names – OPEN/CLOSE
• Overview of Specifications – READ/WRITE
– LR
• How to Code/Compile/ Run
– DO WHILE/ENDDO
RPG program – RETRN
• RPG Indicators – SETON/SETOF
• Introduction to Logic Cycle • Simple examples
Introduction to RPG
• Invented by IBM in early 1960s
• RPG is a high-level programming language
• Most popular programming tool on AS/400
• Procedural language
• Highly position dependent
RPG History
• RPG (Report Program Generator) introduced by IBM in
early 1960s
• Began as a language to produce commonly needed business
reports
• Originally started with fixed-logic cycle and built in
indicators
• IBM started enhancing RPG by adding more functionality as
the usage became widespread
• Programmers started using for complex business processing
RPG History (Cont.)
• Versions of RPG
– RPG (1960s)
– RPG II (released on System/3 during early 1970s)
– RPG III (released on System/3 in 1979)
– RPG/400 (released on Sys./38 & AS/400 in 1988)
– RPG IV (recent introduction)
– ILE RPG ( “ )
Structure of RPG
• An RPG Program comprises a series of
Specifications
• Each specifications is used for a particular
purpose and has specific set of functions
• 7 specifications are available in RPG
• The RPG/400 source program must be entered
into the system in the order shown (except for E
& L which can be interchanged)
Structure of RPG
• Control Specifications (Header) - H Specs
• File description Specifications - F Specs
• Extension Specifications - E Specs
• Line counter Specifications - L Specs
• Input Specifications - I Specs
• Calculation Specifications - C Specs
• Output Specifications - O Specs
Structure of RPG
• Any of the specification types can be absent, but at
least one must be present
• The following entries are common to all RPG
specifications:
• Page (positions 1-2) and Line (positions 3-5). These are the
equivalent of source line number -used for comments
• Specification type (position 6). The following letter codes can
be used ( H F E L I C O)
• Program Identification (positions 75-80). Must be blank or
program name on the Control specs
• Comment Statements (* in position 7). On a comment statement
or a compiler directive, position 6 may be blank.
Symbolic Names
• A symbolic name is a name that uniquely identifies a
specific entity in a program. Its purpose is to allow
you to access that entity. In the RPG/400 language,
symbolic names are used for the following entities:
Day 5
RPG/400 - Agenda for Day 5
• E Specifications • Special Keywords
– *DATE , * YEAR
• Arrays/Tables – CALL
• Error handling in – PLIST/ PARM/ ENTRY
RPG/400 • Operators
– LOKUP
• String handling
– CLEAR/RESET
• Data Area / Data – COMP
Structure – DSPLY
– XLATE
– COMIT/ROLBK
E Specifications
• Extension specifications describe all record
address files, tables and arrays.
• A maximum of 200 arrays and tables can be
used in a program
• This corresponds to the OCCURS and
VALUE clauses in Cobol or the DIM
statement in BASIC
E Specifications
• Record Address File
– A record-address file is a sequentially organised file
used to select records from another file.
– Only one file in a program can be specified as a
record-address file.
– This file is described on the file-description and
extension specifications
– The file processed by the record-address file must
also be specified on the extension specifications
Tables
• A table is a collection of data organized into
columns and rows
• Similar kinds of data are stored within a
column and the data within a row of a table is
related or ‘belongs’ together
• Typically, the data in the first column of a
table is organised in ascending order to
facilitate finding an item
Tables
• A typical 3 column table is shown below
State Code State Name Tax Rate
AK Alaska 1000
AL Alabama 1200
AZ Arizona 1300
CA California 1400
NY New York 900
….. …….. ….
.
Tables
• Data elements within table need to have
same length, same data type and same
number of decimal positions(for numeric)
• The table is defined in the E specifications
of the RPG program
• An RPG table name must begin with TAB
followed by up to 3 additional characters
Tables
• A simple table of 50 state code is coded in E specs as follows
State Code
AK
AL 6 27-32 36-39 40-42 44 45
AZ
CA Form Type Table Name No. of elements Length of element Decimel Sequencing(A/D)
NY
…..
WA E TABCOD 50 2 0A
Tables
• This table definition allocates memory so that
entire table full of data can remain in memory
through out the program execution
• The data values into the table can be put in two
different methods
– Having the data hard coded in program. This is called
as compile time tables
– Having the data obtained from a separate disk file. This
is called as pre-runtime or pre-execution table.
Tables - Compile time
• Here the data is hard coded in the program
source
• This table data is to be entered at the very
end of the program
• The source line should start with ‘**’ and
blank in the position 1-3 in source code and
must precede the table values
Tables - Compile time
• For example the state codes are to coded as
following
1 2 3
* *
A K
A
A
L
R
6 27-32 36-39 40-42 44 45
A Z Form Type Table Name No. of elements Length of element Decimel Sequencing(A/D)
C A
..
W
..
A
E TABCOD 50 2 0A
Tables - Compile time
• On more way coding the same example is shown
below
1 2 3 4 5 6 7 8
* *
A K A L A R A Z
C A C O C T D E
E TABCOD 4 50 2 0A
Tables - Compile time -Summary
• Compile time tables are useful for relatively small
tables
• And whose data are not likely to change over time
• Any change in the data means you have to modify
the source program, change the table data at the
end of program and recompile it
Tables - Pre-Runtime
• Here the data from a database file is read into the
table
• The RPG program automatically retrieves all the
table data from the file before the processing
begins
• Hence the name Pre-runtime table
Tables - Pre-Runtime
• The E specs requires an additional entry in the
position 19-26 i.e. the name of the file containing
the table data
• The entry in the E specs will is shown below
E STATDATTABCOD 4 50 2 0A
Tables - Pre-Runtime
• Since the program is using a database file , a
definition of that file must find an entry in F Specs
F STATDAT I T F 8 E DISK
Tables - Pre-Runtime-Summary
• This is used more for volatile (frequently changing)
type of data
• The contents of the table can be changed in the run
time tables
• This is done by having the following extra entries in
the F and E specs
– The file designate in the F specs should be Combined file,
rather than an Input file
– And a filename in position 19-28 (To file) has to be
mentioned in the E Specs
LOKUP (Look up)
• The LOKUP operation causes a search to be made for a particular element in an
array or table.
• Factor 1 is the search argument (data for which you want to find a match in the
array or table named).
• It can be: a character or numeric literal, a field name, an array element, a table
name, a named constant, or a figurative constant.
• The array or table to be searched is specified in factor 2.
Operator - LOKUP
• For a table LOKUP, the result field can contain the name
of a second table from which an element (corresponding
positionally with that of the first table) can be retrieved.
• Resulting indicators specify the search condition for
LOKUP. That is specified in positions 54-59.
1 2 3 4 5 6 7 8 9 10
* *
A K A L A S K A
A L A L A B A M A
A Z A R I Z O N A
Tables - Two related Tables
• To reflect this data organization, the two tables are defined on the same line of E Specs
• The second table’s name, entry length are coded in the position 46-57 (decimals in Col
56, Sequencing in 57)
E TABCOD 1 50 2A TABNAM 10
Tables - Two related Tables
• The RPG code to get the information of the State
name looks like
E ARS 1 50 2 0
E STATDATARS 4 50 2 0A
• Array names cannot begin with TAB
Array Vs Tables
• The first major difference is that unlike tables, arrays
can be loaded with values during the course of
program execution. This is called as runtime array
The data for runtime array can come from fields of
an input file of a program or from results of
calculations
• Secondly, unlike tables elements, individual
elements of an array can be directly referenced and
manipulated using index or pointer
Array - Run time
A normal runtime array is coded as follows
6 27-32 33-35 36-39 40-42 44 45
Form Type Array Name No of entries /record No. of elements Length of element Decimel Sequencing(A/D)
E QTY 12 5 0
• The positions 33-35 must be blank for a run-time array and this
identifies as runtime array
• The above example is an array ‘QTY’ with 12 elements and of
size 5 numeric
• You can refer to any of the element directly using the index
Array - Run time (Cont.)
E QTY 12 5 0
E QTY 12 5 0
E SAL 12 5 0
PGM4
RETRN
CALL PGM2
PGM3
CALL PGM3
RETRN
CALL PGM4
RETRN
CALL
• The CALL operation passes control to the
program specified in factor 2
• You can specify the library name and it must be
immediately followed by a slash and then the
program name (for example, 'LIB/PROG'.)
• The total length of a literal, including the slash,
cannot exceed 8 characters. The total length of a
field or named constant, including the slash,
cannot exceed 21 characters.
CALL
6 18-27 28-32 33-42 43-48 54-55 56-57 58-59
Type Factor 1 Operation Factor 2 Result HI LOW EQ
C
C CALL Program name PLIST name ER LR
C
C CALL 'PGM1'
C
C CALL 'LIB/PGM2'
C
C MOVE 'LIB/PGM2' CALLX
C CALL CALLX
• RESULT=’CSD DEPT’
• RESULT =‘csd dept’
• RESULT = ‘CSD Dept’
CLEAR
• CLEAR changes the value of the data item(s) to
‘0’ or blanks depending upon the field type
• You can CLEAR individual fields or structures
(data structures, arrays, tables, record formats)
• CLEAR operation requires a factor 2 entry that
specifies which data item is to be cleared
CLEAR
Usage
6 18-27 28-32 33-42 43-48 54-55 56-57
Type Factor 1 Operation Factor 2 Result HI LOW
C
C *NOKEY CLEAR Structure/Variable
C
Error Handling
RPG error handling
No
Exception Execute next
/error seq.instructions
Yes
Day 6
RPG - Agenda for Day 6
• Summarise all Specifications
• More examples
• RPG debugger
• Tips & techniques
• Restrictions of RPG/400
• Introduction to RPG/4
• Exercises
Restrictions of RPG
• Maximum 200 array or tables per program
• Compile time array max input record Len is 80
• Max Character field Len is 256
• Data structure length is 9999
• Data structure occurrence is 9999
• Edit word max 24 for literal & 115 for constant
• Element in array or table max 9999
• File max 50
Restrictions of RPG (Cont..)
• Level of nesting in structured group is 100
• Constant 256 for char, 512 for hex, 30 digit
with 9 decimal for numeric
• Overflow indicator only one
• Parameter max 255
• Primary file only one
• Printer file max 8 per program
• Printing lines min 2 max 112
Restrictions of RPG (Cont..)
• Program status data structure only 1
• Record address file 1
• Record length of program described file is
9999
• Subroutines 254 per program