Professional Documents
Culture Documents
Free-Form RPG Study Notes
Free-Form RPG Study Notes
Free-Form RPG Study Notes
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.
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;
SCOPE OF DEFINITIONS
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
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;