navigation graphic


PHP Resources RPG Resources Debugging Resources RDi Resources Windows 10 & IBM i CL Resources Our Videos Hot Tips!



RPG Definitions

In this article, I wish to take you through defining variables and files in our RPG programs. In an effort to promote total free form RPG; all the examples shown in this article will 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 wanting 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 of my typo's at compile time, not at run-time!

Declaring Files

As I am sure we all know, to do any file handling in RPG we need an F-spec. An example is shown below.

Example 1

// Application Configuration FConfig If E k Disk Rename( CONFI00001 : Fss004r )

In free form RPG, this previous example would be coded, along with a couple of additional screen and printer files, as:

Example 2

Dcl-F Config Rename( ConfI00001 : Fss004r ) ; Dcl-F SqlEx6d1 WorkStn ; Dcl-F SqlEx6p1 Printer ;

Let me explain. The free-form file definition statement starts with DCL-F and is followed by a file name and then by keywords & ends with a semicolon.

Keywords are determined by a default, if it is not explicitly declared. In the example below, the specification does not need the Disk keyword, as this is a default.

Example 3

Dcl-F Config Rename( CONFI00001 : Fss004r ) ; Dcl-F SqlEx6d1 WorkStn ; Dcl-F SqlEx6p1 Printer ; Dcl-F Config Disk( *Ext ) Usage( *Input ) ; Dcl-F SqlEx6d1 WorkStn( *Ext ) Usage( *Input : *Output ) ; Dcl-F SqlEx6p1 Printer( *Ext ) Usage( *Output ) ;

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. Less coding for me to key, 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.

Example 4

$fss_file = Fopen("formaserve.txt","r") or die("unable to open file!"); echo fread($fss_file, filesize("formaserve.txt")); fclose($fss_file);

No use of the RPG cycle there! I will leave that discussion for another day!

Defining Constants

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 and as with all specifications, ends with a semicolon.

Example 5

Dcl-C ABD 'ABD' ; Dcl-C FSS Const('FormaServe') ;

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 uppercase, 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.

Example 6

define("ABD", "ABD") ; define("FSS","FormaServe") ;

In VB, use the Const statement to declare a constant and set its value.

Example 7

Public Cont ABD as String = "ABD" Public Cont FSS as String = "FormaServe"

Defining Standalone Variables

To define a standalone variable, use the Dcl-S specification, the variable name, variable type and length.

Example 8

Dcl-S Count Int( 10 ) ; Dcl-S SQLStatement Char( 1024 ) ; Dcl-S Message Char( 52 ); Dcl-S Found Ind Inz( '1' ) ; Dcl-S Salary Packed( 6 : 2 ) ; Dcl-S UserSpace Pointer ; Dcl-S Date_DMY Date(*DMY-);

Defining variables in PHP is completely different to RPG; PHP is what they call a 'Loosely Typed Language'.

Example 9

$count = 5; $SQLStatement = 'Select * from FSS004' ;

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.

Example 10

Dim Count as Integer = 5 Dim SQLStatement = "Select * from FSS004"

Data Structures

To define a data structure, we start with the Dcl-Ds definition and must end with an End-Ds and each line completed with a semicolon.

The example below shows three different types of data structures being defined.

Example 11

Dcl-Ds dsPsds PSDS ; s1Pgm *Proc ; s1Usr Char( 10 ) Pos( 254 ) ; End-Ds; Dcl-Ds dsRecord ExtName( 'CARP' ) Qualified Inz ; End-Ds; Dcl-Ds dsShipping Qualified ; Name Char( 20 ) ; Address1 Char( 20 ) ; Address2 Char( 20 ) ; Address3 Char( 20 ) ; End-Ds dsShipping ;

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.

Where 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. If you missed my presentation on CL programming at the 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 as in the RPG third example above.

Example 12

In CL programming, the DefVar keyword is utilised to make a sub-set of the shipping address.


Procedures are coded with a Dcl-Proc to start the procedure and completed with an End-Proc statement.

Example 13

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

Example 14

function add($x, $y) { $total = $x + $y; return $total; }

In VB, procedures are called sub-procedures and functions, very similar in syntax to PHP.

Example 15

Function add(x as integer, y as integer) As Integer dim rc as Integer rc = x + y return rc End Function

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 to 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 these, in the future. If they do, 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 now.

Personally, I see the take up of this latest version quite occasionally, so personally I take every opportunity to promote what has been my main, and favourite, programming language for the past 35 years.

  follow us on twitter   find us on facebook   follow us on linkedin   see our videos   find us on google+