I would like to take you through the process of defining variables and files in RPG programs. In an effort to promote totally free-form RPG, all the examples shown in this article detail the latest enhancements.
Additionally, I will show you equivalents to RPG in other programming languages. I hope that this will give a push to the younger programmers who want to develop and maintain programs on the platform we all love.
Thankfully, RPG is not a language where it is optional to define variables, as in PHP, VB etc. I need to know about my typos at compile time, not at run time!
As I am sure you know, to do any file handling in RPG we need an F-spec. An example is shown below.
In free-form RPG, this previous example would be coded along with a couple of additional screen and printer files as:
Let me explain. The free-form file definition statement starts with DCL-F and is followed by a file name and then by keywords and ends with a semicolon. Keywords are determined by a default if they are not explicitly declared. In the example below, the specification does not need the Disk keyword, as this is a default.
The most common device type is DISK so this is a default; we do not have to code it. Additionally, most files are externally described so, again, this is a default for files.
The most common usage depends on the device type. For example, disk files are input files whereas printer files are output only. IBM has carried out an excellent job in allowing us to use these defaults and less coding for me to key equals one happy programmer.
Declaring files is quite unique to the RPG programming language. PHP, VB, C# will just use the relevant operational code. The PHP code below shows a file being opened, read and closed.
No use of the RPG cycle there! But I will leave that discussion for another day…
To define a constant in RPG, we use the Dcl-C which is then followed by the name, then by the optional keyword CONST, and then by the value which, as with all specifications, ends with a semicolon.
There is no difference in meaning between specifying the CONST keyword and omitting it. Constants can be defined either globally or locally in a procedure. Generally, I always code constants as upper case – just my personal preference.
In PHP, constants are declared using the DEFINE function. The example below shows the PHP equivalent to the RPG constants’ definition.
In VB, use the Const statement to declare a constant and set its value.
Defining standalone variables
To define a standalone variable, use the Dcl-S specification, the variable name, variable type and length.
Defining variables in PHP is completely different to RPG. PHP is what they call a “loosely typed language’.
In the example above, notice that we did not have to tell PHP which data type each variable is. PHP automatically converts the variable to the correct data type, depending on its value. In VB, the DIM statement is used to define variables.
To define a data structure, we start with the Dcl-Ds definition and must end with an End-Ds and each line is completed with a semicolon.
The example below shows three different types of data structures being defined.
In the first example, we have declared a data structure picking up the program system data structure, called dsPsds, which contains two subfields, s1Pgm and s1Usr. The second one is a fully qualified data structure that uses the file layout of our car file CARP to define the data structure. The third example is a typical data structure being utilised for address details.
As with file specifications, RPG is quite unique in having the functionality of data structures. The only other programming language I know that has this type of feature is CL programming. (Note to i-UG members: if you missed my presentation on CL programming at the recent International iPower conference where I explained this feature, it can be downloaded here.)
An example of using this functionality in CL is shown in the figure below. It is the equivalent coding to the RPG in the third example above. In CL programming, the DefVar keyword is utilised to make a subset of the shipping address, as shown in the example below.
Procedures are coded with a Dcl-Proc to start the procedure and completed with an End-Proc statement.
An example of the prototype, which is started with Dcl-Pr and ended with End-Pr, is additionally shown. Therefore, this prototype has the supplier number, defined as an integer, with a length of 10 being passed to the procedure “isSupplierFound”, which returns an indicator on whether the supplier record is located.
In PHP, procedures are coded as functions:
In VB, procedures are called sub-procedures and functions – very similar in syntax to PHP:
Still not 100% free!
While well on the way to totally free RPG, there are still restrictions that apply. These are:
* Free Form is still restricted to columns 8 → 80.
* I & O Specs must still be coded in columns (yes, I still use them and no, I cannot get around it).
* RPG Cycle must be coded in fixed-format.
Hopefully, IBM will remove these limitations, or some of them, in the future. If it does, we will be one of the first to let you know.
This article intended to promote the free-form RPG that has been available to us for over 18 months. Personally, I only see the take up of this latest version occasionally, so I take every opportunity to promote what has been my main – and favourite – programming language for the past 35 years.
Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems.