ILE RPG, RPG IV Variables (Character, Packed and Zoned Numerics)

Variables And Constants in RPGLE

Variables in RPG were declared at the time of using them. After RPG IV was introduced
variables are now defined in the D Spec. Though IBM has not restricted you from defining the variables while using them, but you can do that in only RPG statements. It means that though you can define a variable while using the MOVE op-code, you can not define it if you choose to use the equivalent Eval Statement in RPGLE. So, all the variables and constants in this tutorial have been
define in Declration Specification.

Now We will learn how to define variables and constant in D Specification. But before that
let us know what are the common variables and data types available in RPGLE.

We have the following data types sometimes also called as data formats in RPGLE

  • The character type variable. This variable is represented by 'A' (without quotes)
  • The numeric type variable. Here we have basically two data types.
    • The Packed number. This is the default numeric data format. If you do not mention
      the data type of a numeric variable then the system will treat it as numeric data type. This is the
      most widely used data type. This data type is represented by a 'P' in RPGLE programs. However the packed type number is
      represendted by a 'Y' in DDS.
    • The Zoned number. The Zoned data type number is represented by 'S'.

    For all purposes within a program there is no difference between these two data types.
    However they created problems when they are passed as parameters to other programs by giving generally decimal data errors. To avoid decimal data errors just make sure that the data type of numeric variable expected by the called program is same as the data type of variable being passed to it.

  • RPGLE has some other data types like pointers, integers, floats and graphic etc. But they are out of the scope of this basic tutorial. This tutorial is actually for beginners of ILE RPG (RPG IV).

Note:- If you are from java or some other similar language background, please do not be confused with the float of java with the float of RPGLE. You can specify the decimal place of a packed or zoned numeric variable as 2 and it will become equivalent to a float of java language. Similarly if you fix the decimal place of a numeric variable to be zero (0) it will be like int of java!.

Now let us learn how to define the variables in RPG IV or (RPGLE). As discussed above, the variables will be defined in the D spec only. Also from the previous page you know that all the RPGLE specifications begin from the 6th position. So a variable is defined as below.
Declaration of a Character type Variable in RPGLE.

DName+++++++++++ETDsFrom+++To/L+++I
DW@Char           S             10A

The first line is just for your help. You can display this line anytime by the F command in SEU. Anyways, the above line declares a variable of standalone in nature. (Okay, statndalone means that this variable is not dependent on any other variable for its definition.
We have some variables which are dependent on other variables for their definition. For example the prototypes' parameters, the data structures' subfileds, the like defined fields etc).
The length of the stand alone variable in the above example is 10. You can refer to this variable any time in your program using the name W@char.

Declaration of a Packed numeric type Variable in RPGLE.

DName+++++++++++ETDsFrom+++To/L+++IDc
DW@Packed1        S             10P 0
DW@Packed2        S             10  0
DW@Packed3        S             10P 5

Here both the statements declare a packed numeric variables. The first declaration declares a packed variable of length 10 with zero decimal places. The second variable again does the same. However the third declaration statement declares a packed variable of length 10 with 5 numbers after the decimal places. So the three variables can store the following maximum values.

W@Packed1 = 9999999999
W@Packed2 = 9999999999
W@Packed3 = 9999.99999

Clear? If you try assign a value greater in length as compared to the length of a variable, the program will throw an error 'Target value is too small to hold the result'.
So the following statement will throw the above mentioned error.

W@Packed3 = 9999999
The acceptable values here would be anything between -99999.99999 to 99999.99999 (999 is acceptable too. This will be converted to 999.00000 after assignment!).


Declaration of a Zoned numeric type Variable in RPGLE.

DName+++++++++++ETDsFrom+++To/L+++IDc
DW@Zoned1         S             10S 0
DW@Zoned2         S             10S 5

After learning the packed variables, the zoned variables' declaration should not be problematic to you. The first RPGLE statement W@Zoned1 declares a zoned numeric variable of length 10 with zero decimal places. The second statement declares the a variable W@Zoned2 of total length 10 with 5 decimal places.



Initializing the Variables

Just like any other programming RPGLE also has facility to initialize a variable. (By initialization we mean that the variable will be having the initialized value from the very beginning, even before you assign it any value by using some assignment opcode!) To initialize any RPGLE variable you should just use the keyword Inz as given in the example code below.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++
DW@Char           S             30A   Inz('Tutorial India!')
DW@Packed1        S             10P 0 Inz(5)
DW@Zoned1         S             10S 5 Inz(5) 

You can use the opcode DSPLY to display the values.

The maximum length allowed to declare an RPGLE variable name is 10. The RPGLE variable names are cAsE inSenSitive. So W@Packed1, W@packed1 and w@Packed1 all are same. An RPGLE variable name must begin with any character or $,or @ sign. However you can use the numbers and _ (underscore) sign afterwards.


Program Summarizing All the above RPG IV data types
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++
DW@Char           S             30A   Inz('Tutorial India!')
DW@Packed1        S             10P 0 Inz(5)
DW@Zoned1         S             10S 5 Inz(5) 
CL0N01Factor1+++++++Opcode
C     W@Char        Dsply
C     W@Packed1     Dsply
C     W@Zoned1      Dsply
C                   Return

The above program will display the values 'Tutorial India' 5 and 5.00000 one by one.