Standalone variables (sometimes called work fields) are not part of a database record or any other kind of data structure. Miss the previous excerpts regarding to using declarations? Read each here: Part 1 & Part 2
Editor's Note: This article is excerpted from chapter 4 of Programming in ILE RPG, Fifth Edition.
They stand alone in the program, without depending upon any other kind of data item. A typical use for a standalone variable might be as a counter to count the number of transactions being processed, as an intermediate variable to temporarily hold a value for later processing, as an accumulator to keep running track of year-to-date sales amounts, or as an indicator to represent whether a condition is true. Unlike a named constant, the value stored in a standalone variable might change often while the program is running. The Dcl-s (Declare Standalone Variable) instruction defines a variable:
Here are a few examples of definitions for standalone variables:
Data Item Name
The name must conform to the rules governing data item names, the same rules used for named constants. The name should reflect the contents of the variable. Data item names can be up to 4,096 characters, but it’s best to restrict them to a manageable length. If the variable is to be used in an externally described file, its name should be 10 or fewer characters.
Data Type
The attributes for a variable are described in a data type keyword, following the variable’s name. You’ll recall from Chapter 3 that the three basic classes of data used in most business programming are numeric, character, and date data. RPG supports several variations of these data classes, along with a few others that have special uses. The following are most frequently used data type keywords:
- Char, Varchar, Ind
- Zoned, Packed, Int, Uns
- Date, Time, Timestamp
- Pointer
Most of these data types were discussed in Chapter 3, but ILE RPG supports several others as well. This chapter expands on some of the additional data types that are not directly related to SQL or DDS. Chapter 18 covers the Pointer data type.
Char, Varchar, Ind
When a declaration defines a variable as Char, it is defining a fixed-length character string. You specify the length attribute in parentheses. All values in the column have the same length in storage. A value that doesn’t fill the entire length is padded with blanks. The following definition defines Address, a fixed-length character variable 35 characters (bytes) long:
The length can be between 1 and 16,773,104 bytes. You can specify either a literal or a named constant (but not another variable) for the length. If you stipulate a named constant, you must define the constant in the program source before defining the variable:
If the variable is defined with a Varchar data type, it is a variable-length character string. Recall from Chapter 3 that this data type is appropriate when the length is uncertain. You specify the maximum size (up to 16,773,100 bytes) in parentheses. As with a fixed-length character string, the length can be a literal or a named constant (which you define before the variable). The following declaration defines Email, a variable-length character column 1 that is 256 characters long:
Chapter 7 examines variable-length character strings in more detail.
Note
You may have noticed that the maximum sizes for RPG variables are larger than the limits for fields defined by SQL or DDS. Although the database might not support some very large fields, character data items defined in an ILE RPG program generally have higher limits, up to about 16 MB.
A third character data type, Ind, defines an indicator. A program uses an indicator, sometimes called a flag or a Boolean data type, to signal a true/false state. An indicator is a single-byte character field that can have only two possible values: *On ('1') or *Off ('0'). The program can set indicators on or off, and then subsequent processing can be conditioned by the state of the indicator. The following declaration defines an indicator that should be familiar to you by now, Endofpage:
Because all indicators are one byte, you do not specify a length for an indicator.
Zoned, Packed, Int, Uns
Recall from Chapter 3 that zoned (or signed) variables store numeric data, with each digit occupying a single byte, and that packed variables use only a half byte for each digit. RPG supports both numeric data types:
The parameter values inside the parentheses specify the precision (total digits) and scale (decimal digits), respectively. Maximum precision and scale are both 63 digits; the precision must be at least as large as the scale. Either or both of the values can be a literal or a named constant (which you define before the variable). If you do not specify a scale, it defaults to zero. The following declarations are equivalent:
Zoned numbers are equivalent to SQL’s NUMERIC data type, and Packed numbers are the same as DECIMAL in SQL.
A numeric variable defined with Int is a signed integer. Recall from Chapter 3 that integers are the most compact means of storing numeric values. The following declaration defines a five-digit signed integer, Counter:
The length in parentheses (which can be a literal or constant) must be 3, 5, 10, or 20 digits:
Digits |
Bytes |
Lowest Value |
Highest Value |
3 |
1 |
-128 |
127 |
5 |
2 |
-32,768 |
32,767 |
10 |
4 |
-2,147,483,648 |
2,147,483,647 |
20 |
8 |
-9,223,372,036,854,775,808 |
9,223,372,036,854,775,807 |
Integers have no decimal places and have limited ranges. If, however, the anticipated data values fit into the allowed range and scale for any of the integer sizes, they are an efficient and useful data type for numeric data. A five-digit integer is equivalent to SQL’s SMALLINT, a 10-digit integer is equivalent to INT in SQL, and a 20-digit integer corresponds to BIGINT.
A variation on signed integers—Uns—defines an unsigned integer. Unsigned integers follow the same principles as do signed integers except that their values are always positive. The following declaration defines Counter, a five-digit unsigned integer:
Unsigned integers have a range of values different from signed integers:
Digits |
Bytes |
Lowest Value |
Highest Value |
3 |
1 |
0 |
255 |
5 |
2 |
0 |
65,535 |
10 |
4 |
0 |
4,294,967,295 |
20 |
8 |
0 |
18,446,744,073,709,551,615 |
Date, Time, Timestamp
The RPG date-related data types correspond to the SQL types. The following declarations define date and time variables called Today and Now:
By default, timestamps include microseconds (six digits to represent fractional seconds):
If you need a different scale, the variable definition can include a length parameter 0–12 to represent the desired number of fractional seconds’ digits:
Assigning Initial Values to Data
In addition to defining data items, such as standalone variables, you can assign an initial value to those data items. If the data item is a variable, its value can change during program execution, but its initial value is the one the variable contains when the program starts.
To initialize (i.e., assign an initial value to) a standalone variable, specify the value by using the Inz (Initialize) keyword in the variable’s definition. You indicate the initial value by using a literal, a named constant, or a figurative constant. The following definitions all provide initial values to the variables:
You can use some special reserved values to initialize date-related definitions. To initialize a date field to the job date (the same as *Date in the previous examples), code Inz(*Job) in the keyword area of the field’s definition. You can also initialize a date, time, or timestamp field to the current system date at runtime by coding Inz(*Sys). What’s the difference between the job date and the system date? You can think of the job date as being an as of date that is assigned to a job when you run a program, although it may or may not be the actual date that the program is running. The system date, however, is always the current date:
One other useful initialization value, *User, can be used with character fields if they are at least 10 bytes. Coding Inz(*User) for a character field assigns the name of the current user profile to the character field:
Know that it is not always necessary to assign an initial value to a data item. RPG automatically initializes data items to default values when the program starts unless you use the Inz keyword to initialize the variable. The default values are typically blanks for character variables and zeros for numeric variables. If the default values are sufficient, you need not initialize the data item, as the following examples show:
Remember that the Inz keyword assigns only a variable’s initial value. In subsequent chapters, we discuss how you can assign new values to a variable once a program is running.
Next time: Defining Data Structures. What to learn more now? Buy Programming in ILE RPG, Fifth Edition at the MC Press Bookstore today!
LATEST COMMENTS
MC Press Online