Free-Form RPG Study Notes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

 Name the seven RPG specification types and describe the function of each.

 Definition (D spec): these are statements that defines a data item.


 Calculation (C spec): these are all executable statements.
 File (F spec): these are statements that defines a file to be processed the
program.
 Note: RPG process two types of files –externally
described files and program described files.
 Control (or header)(H spec): these statements defines defaults for the
program.
 Input (I spec): these statements describes the records in a program-
described file to be used for input.
 Output (O spec): these statements describes the records in a program-
described file to be used for output.
 Procedure (P spec): these statements is used to declare a main procedure
or sub-procedure within an RPG program.
o A program need not include every specification type, a program can contain only
F, D, and C specs. C specs must be coded first, F or D specs second (in any
order), I spec third, C spec fourth, and P specs last.

o A source program consists of a series of statements. Statements that define data


items or files to be used by the program is called non-executable statements.
Other statements that defines an operation to be performed are called executable
statements
o

 Describe the differences between free-form and fixed-format RPG statements.


 Free-form format: columns 1-5 can be used for an optional sequence
number. Column 6 and 7 must be left blank. The statement itself must be
coded within columns 8 – 80. Every free-form statement begins with an
operation code (op code) E.g. DCL-F. Multiple statement cannot be coded
in the same line. Every free-form statement must end with a semicolon (;),
only comments, which begins with a double forward slash (//), can be
coded after the semicolon. Free-form RPG statements can be coded on
multiple lines, no continuation characters are need. Line continuation is
only needed when you want to split a literal on multiple lines.

 Fixed-format: Keywords and parameter values must be coded in specific


columns. Column 1-5 are ignored by the compiler and therefore can be
used for comments or line numbers. A character in column 6 indicates the
specification type, an asterisk in column 7 makes the statement a comment
and causes it to be ignored by the compiler. Although there is a precise
format for the first 43 columns of a definition specification, columns 44
through 80 can be used to specify keywords that further define the
character characteristics of the data item. These keywords can begin and
end in any column within columns 44 through 80.

 Mixing Formats: An RPG program can consist of both fixed and free-form
statements. The compiler determines which format to use by checking
column 6. If it is blank, free-form format rules are used, if it contains a
character, then fixed-format rules are used.
 Note: I and O specs are used for program-described
files.

 RPG /FREE: Between 2001 and 2014, the only RPG statements that could
be coded using the free-from format were control specifications (c specs).
The /FREE compiler directive, which must begin in position 7 or later,
was used to indicate the beginning of a free-form C spec group. The
/END-FREE compiler directive indicates the end of the free-form C spec
group.

 Fully Free-From: If a source file contains only free-from RPG statements,


it is said to be fully free-form. You tell the compiler you want to only use
free-form by coding **FREE beginning in column 1 of the first line of the
RPG source program. When **FREE is defined in your code, you are then
allowed to start coding from column 1 or later, also, the 80-column limit is
lifted. Without **FREE, a source file is assumed to include column-
limited statements, as a result, free-from statements are restricted to
column 8 – 80. Note: the /FREE and /END-FREE directives are no longer
required as of TR7 (technology refresh 7). Also, an error will be raised
if /FREE and /END-FREE directives are coded in a fully free-form
program.
 Define the following terms as they pertain to the RPG language:
o Operation code: Is a name recognized by RPG that identifies the purpose of a
statement. These codes indicated the type of operation to be performed within a
C-spec statement such as READ,WRITE, IF , and EVAL. All the other
specification types performed a single function (such as declaring a file or a data
item), so the concept of an operation code was not needed. The specification type
said all that was necessary. With the introduction of free-form statement, the
specification letters coded in column 6 (H,F,D, and P) are no longer there. They
have been replaced with the names CTL-OPT, DCL-F,DCL-S, DCL-DS, DCL-P,
and others. The term “operation code” seemed appropriate for these names. There
is a distinction, however, between the operation codes used in C specs and those
used in all other specification types. In particular, many of the C spec op codes
support an operation extender, which defines additional attributes of the
operation.
o Built-in function: the name of every built-in function begins with a percent
symbol (%). A built-in function is evaluated and yields a value at execution time.
E.g. %EOF
o Indicator:
o Literal: A literal is a self-defining data item that can be referenced on a
specification. A literal is normally used to specify an initial value. One difference
between a character literal and a numeric literal is that a character literal value
must be enclosed in apostrophes (‘). Also, any character can be specified in a
character literal, while a numeric literal can consist only of digits, decimal
notation character, such as a decimal point, and a leading plus or minus sign.
Note: to specify a literal that contains an apostrophe, you must specify an
additional apostrophe E.G. to specify o’clock as a literal you have to type
(‘o’’clock’). You can continue a non-numeric literal on multiple lines using a
continuation character (- or +). Numeric literals can’t be continued onto multiple
lines. The ‘+’ continuation character continues a literal at the first non-blank
character at or after the first column of the second line. The ‘-‘ continuation
character continues a literal at the first column of the second, blanks are not
ignored.

o Figurative constants
o Reserved word
o Compiler directive
 Describe the rules for RPG statements continuation
DEVELOPING A MODULAR APPLICATION
 What is referred to as modularizing and application
o The process of breaking up an application into smaller pieces is modularizing the
application. The resulting application will consist of a main process and a group
of sub-processes
 Identify the advantages of modularizing an application
o A process be shared and used by multiple applications.
o An installation can accumulate a library of tools that application developers can share.
o A sub-process is designed, coded, and debugged once.
o A complex problem is easier to understand when you can focus on one small part at a
time.
o Applications become faster and easier to design because different programmers can
code different parts(modules) of the application simultaneously.
o Modular applications are easier to modify later, because only a single process needs to
be changed.
o The MVC(Model/View/Controller) design approach is another form of modularization.
With this approach, the code used to model and manipulate the data, display output,
and make control decisions are put into separate sub-process. This approach makes it
easier to make major changes later, such as replacing a green-screen interface with a
web interface. In this case, only the “view” process needs to be changed.
o Using a sub-process for all I/O involving a particular file means that only that sub-
process needs to be changed to move from traditional RPG I/O statements to embedded
SQL statements.
 Identify and describe the methods that can be used to modularize an application
o An application can be modularized by using sub-routines or sub-procedures.
o Sub-routines are coded following the calculation specification for the main
routine in the program and is invoked by using the EXSR op code.
o Sub-routines cannot include local data items, as a result, they are not self-
contained, therefore they are only used when the function performed by the sub-
routine is not required by multiple programs. Sub-procedures, however, can
include local data items, as a result, they are self-contained and therefore can be
compiled separately which allows them to be use by multiple programs,
o The main part of an RPG program (including its subroutines) is called the main
procedure. This is the part of the program that initially receives control when the
program is called.
o There are two types of main procedures: cycle-main procedure (which executes a
set of codes behind the scenes), and linear-main procedure which do not require
special startup codes. If a main procedure includes the CTL-OPT keyword MAIN
then the procedure is a linear-main procedure, the NOMAIN key word is used to
tell the compiler that a module only includes sub-procedures. if a main procedure
does not include the CTL-OPT code , then it is a cycle-main procedure.
o A procedure that invokes another procedure is called the calling procedure.
o Sub-programs can be used to implement a modular design instead of subroutines
and sub-procedures. Sub-programs are compiled as a separate *PGM object. It is
called by the program that require its processing, a sub-program is the main
procedure of its own program object “sub” just indicate that it is called by another
program.
o A program that invokes a subprogram is called the calling program, this program
need not be written in the same programming language as the sub-program. When
a subprogram call is encountered, the operating system locates the sub-program
and passes control to it. This process is called program activation.
o Invoking a subprogram involves a dynamic call, which requires large amounts of
system overhead thereby affecting system performance. Invoking an external
procedure, however, requires a static call, that does not require program
activation. Because all procedures used by the program are activated when the
main program is activated, as a result, all new RPG applications should be
modularized using sub-procedures and/or external procedures.
 Describe the techniques that can be used to allow a calling program or procedure to
communicate with a subprogram or called procedure
o The most common means of communication between a caller and a callee is by
passing a parameter. A caller can pass a parameter to a callee by reference, value
or read-only reference.
o When a parameter is passed by reference, the address of the data item in the caller
is passed to the callee, thereby providing access to the actual data item within the
caller. When a parameter is passed by value, the compiler supplies the callee with
a copy of the data item located in the caller, instead of the actual data. As a result,
no changes can be made to the actual data by the callee. Passing a parameter by
value, however, can’t be used to pass values to a main program or subprogram.
Passing a value by read-only reference is the same as passing by value, except
that the address of the data item in the calling program is passed to the callee,
however, the callee is not allowed to update the parameter. Unlike passing a
parameter by value, passing a parameter by read-only reference does allow you to
pass a value to a sub-program or main procedure.
o The system can guarantee that the parameter value will not be changed only if the
called sub-program or procedure is written and compiled in a language that
enforces the read-only reference method. If the callee is written in another
language, then the system cannot ensure that the parameter is not changed.
CODING AND USING A PROCEDURE
 Describe the requirements of a prototyped call
o Errors can occur when passing a parameter from a calling program to a called
program. These errors are as follows:
 An incorrect number of parameters were passed.
 Parameters were passed in an incorrect order.
 The data type of the Parameters passed were incorrect.
o All calls are prototype calls, such calls require the coding of the definition of the
call interface. This interface must also be specified in both the module containing
the calling program and the module containing the sub-program or called
procedure. All prototype calls are invoked by the CALLP op code
 Code a prototype definition
o A prototype definition includes:
 The type of call (dynamic or static)
 The location of the program or procedure
 The number and order of parameters
 The format of any optional return value
o When a program containing a prototyped call is compiled, the compiler verifies
that the call itself matches the information from the prototype definition, if it
doesn’t an error is thrown.
o To assure that the prototype definition is correct for a called procedure or
program, the definition is coded and stored in a separate source file, where it can
then be copied into both the called and calling program using the /COPY compiler
directive.
o A prototype is defined by coding the DCL-PR op code, the name and data types
of the parameters and the END-PR op code to signify the end of the prototype
definition. E.g.
 DCL-PR PrototypeName;
 NameOfParameter1 data type;
 NameOfParameter2 data type;
 END-PR;
 The name assigned to a parameter field in a prototype declaration
serves as documentation only. Different names can be used for the
data item in the CALLP statement and within the called procedure
or subprogram. If the name of the parameter has a name identical
to a reserved word or op code, then the parameter must be
preceded by the DCL-PARM op code.
o To indicate that a parameter should be passed by value, the VALUE keyword
must be coded after the name and data type of the parameter. Use the CONST
keyword instead of the VALUE key to pass a parameter by read-only reference.
 Code a subprocedure, external procedure, or subprogram
o A sub-procedure differs from the main source section of a program in that a sub-
procedure can include on F,D,C specs. All files and data items defined in a sub-
procedure is considered local to that procedure and can’t be referenced outside of
the procedure.
o The DCL-PROC keyword is used to begin a procedure, and it is ended by coding
END-PROC. Note: specifying the name of the procedure after the END-PROC op
code is optional.
o Every procedure that accepts parameters must include a procedure interface. This
interface is coded just like the prototype definition except you begin the interface
by coding the DCL-PI op code instead of DCL-PR. And it is ended by END-PI.
Note also that the parameter coded in the procedure interface must be identical
prototype definition in number, data type, and order.
o The name of the procedure interface should match the name of the procedure
itself, or you can use *N to avoid having to type a name.
 Call a subprocedure, external procedure, or subprogram
o The CALLP op code is used to call a prototyped procedure aka prototype call. E.g CALLP
NameOfPrototype(parm1:parm2);
o Note: the CALLP op code can be omitted of if no operation extenders are needed, and if
the prototype does not have the same name as an operation code.
o If the called procedure does not accept parameters, it can be coded with or without the
parentheses E.G.:
 CALLP INIT();
 CALLP INIT;
 INIT();
 INIT;

USER DEFINED FUNCTION

 Define a user-defined function


o A user-defined function is a sub-procedure that returns a value without using a
parameter.
 Code a user-defined function
o To code or transform a procedure to a user-defined function, you must remove the
parameter that is returning the value from within the DCL-PR and DCL-PI
statements. Then code the data type of that parameter on the same line as the
DCL-PR and DCL-PI op codes. Then code the name of the parameter that was
removed at the end of the RETURN keyword.
o It is more efficient to return a very large data set by reference then by a function
call. Using the RTNPARM keyword, however, causes a value or data set to be
returned by reference even though a function call syntax is used. E.g. DCL-PI
procname datatype RTNPARM END-DCL;
 Invoke a user-defined function
o The CALLP op code is used to call a prototyped procedure, but not for procedures
that returns a value, such as a user defined function. To invoke a user-define
function, you must code it the very way you would a user-defined function.
 Code an external procedure, or subprogram
o To code an external procedure code OPT-CTL NOMAIN in the main source
section of the module. Then include EXPORT on the DCL-PROC op code after
the procedure name. E.g DCL-PROC checkcode EXPORT;
o If MAIN or NOMAIN was not specified on the OPT-CTL op code, then the
compiler will treat the program as a cycle-main program. By default, all cycle-
main program are exported, so you don’t have to code the EXPORT keyword.
o A sub-program cannot be converted to a user-defined function, therefore you
cannot use it to return a value via the RETURN statement as you can with a sub-
procedure, the only way to return a data is via a parameter passed by reference.
 call an external procedure, or subprogram
o you can call a procedure by a different name by using the EXTPROC keyword as follows:
DCL-PR checkcode EXTPROC(‘xyzzy’); The name of the procedure is XYZZY but checkcode
would be used to reference it, so.. the code to call this procedure will be CALLP
CHECKCODE;
o Use EXTPROC(*DCLCASE) on a DCL-PR statement to prevent RPG from converting all
letters to upper case.
o Some parameters are handled differently by CL programs as compared to an RPG
program. To call an external procedure written in CL into in RPG use the
EXTRPOC(CL:procname) on the DCL-PR statement, where procname is the name of the
procedure to be called into RPG.
o To call a sub-program you must use the EXTPGM keyword as follows DCL-PR
NameOfSubProgrm EXTPGM; To call a sub-program by a different name, use the
following code DCL-PR NameToUse EXTPGM(‘actualNameOfTheProgram’);

SCOPE OF DEFINITIONS

 Distinguish between local and global files and data items


o Global files and data items are coded in the main source section of a module,
which is before the first DCL-PROC statement. They can be accessed by any
procedure in the same module.
o Local files and data items are coded within a procedure; they can only be accessed
by the procedure in which they were defined.
o If a local and global data item has the same name, any reference to the data item within a
procedure will use the local data item.

 Export and import data items


o A data item is said to have a global scope if it is defined in the main source
section of a module. These data items, however, are global only to procedure
within the same module and not the entire program. To cause a data item to be
global to the entire program, the export and import keyword must be used. These
keywords allow S a data item to exported from one program and imported into
another. Thereby causing the data item to be global to the entire program it was
imported into.
o Export and import can only be used for global data items and not local data item.
o Storage is not allocated for data items that has an IMPORT keyword defined.
Reference to the data item are said to be unresolved, however, the binder searches
for other modules for an exported data item with the same name. Once found, the
binder resolves the external reference to the data item in the importing module by
pointing them to the data item in the exporting module.
o Use EXPORT(*DCLCASE) or IMPORT(*DCLCASE) to prevent RPG from
changing the name of a data item to upper case, in other words, leave data item
name as is.

 Distinguish between static and automatic storage


o By default, local data items are created in automatic storage and is allocated
each time a procedure is called and released when it ends. They are initialized
each time the procedure is called. Note: the value of a local variable can also be
made static, by using the static keyword.
o On the other hand, global data items are stored in static storage, and are
initialized when the module is activated, not when a procedure is called. Their
values are retained for the duration of the program’s run time.

 Define a local variable that is kept in static storage


o DCL-S myvar char(10) INZ(‘Lynch’) static;

 Describe how an RPG program or procedure controls whether the value of a data item is
retained between calls
o This is achieved by specifying an activation group (ACTGRP) on a stand-alone
field. Note: specifying ACTGRP(*NEW) signifies that the activation group does
not have a name on a data item causes the data item

 Define a file whose control information is kept in static storage


o DCL-F MYFILE STATIC;

ADVANCED PARAMETER CONSIDERATION

 Code a main program that accepts parameters


o Character constants specified on a CL CALL command has a minimum length of
32. Therefore a 7 character constant such as ‘Desmond’ will be represented as
“Desmond “, with blanks padded on the right.
o When a decimal constant is passed as a parameter, it is treated as a packed
decimal number with a length of 15 digits. Therefore a decimal constant of 945
will actually be represented as 0000000945.00000
o When receiving values from a CL program, always remember to cater for the way
CL handles constants E.G. receiving a 3 digit number from a CL program
remember to set the variable length as PACKED(15:5) and not PACKED(3).
Apply the same concept when receiving character constants.

 Code a procedure that accepts a variable number of parameters, including no parameters


o You specify the OPTIONS keyword with the value *NOPASS to indicate that a variable is
optional or not required. E.g. MYVAR CHAR(10) OPTIONS(*NOPASS);
o No other required parameter can be specified AFTER an optional perimeter was defined,
only additional optional parameters can follow. You can get around this, however, by
using OPTIONS(*OMIT), when that is the case, the calling program may specify *OMIT
for the value of the parameter. As a result, the position occupied by the parameter is
still used; it just contains the string “*OMIT”. Note also that *OMIT is only allowed for
parameters that are passed by reference or by read-only reference. RPG does not pass
an address to the called procedure, instead, the address field is set to null. The %ADDR
function is used to determine the address of a data item. E.g IF %ADDR(PARM2) =
*NULL;

o The %PARMS function can be used to obtain the number of parameters that were
passed or defined in a procedure. E.g IF %PARMS >1;

 Code the keywords to trim and adjust parameter values


o Use OPTIONS(*TRIM) to remove all leading and trailing spaces from a passed
parameter, the result will be left justified.
o Use OPTIONS(*RIGHTADJ) to right-adjust characters passed, it only has an
effect when the calling procedure passes a string longer than what the called
procedure expects.
o Multiple can be passed as follows OPTIONS(*TRIM:*RIGHTADJ);
 Code a procedure that accepts a variable-length parameter
o Use OPTIONS(*VARSIZE) to specify that the passed string can be longer or
shorter than what is defined in the procedure or prototype
 Pass a file to a procedure
o Use the LIKEFILE keyword to specify that a file will be passed as a parameter
within a procedure. E.g MYFILE LIKEFILE(the name of the external file);
o When a file is named in a LIKEFILE keyword, it must be first declared using the
BLOCK keyword. E.g. DCL-F MYFILE BLOCK(*NO), by coding BLOCK
explicitly, you control whether blocking is performed for all procedures to which
the file is passed. If the procedure is to be complied separately, the TEMPLATE
keyword MUST be on the file declaration. E.g. DCL-F MasterFile BLOCK(*NO)
TEMPLATE;

You might also like