Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 1

Here are a few examples of definitions for standalone variables:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 2

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 3

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 4

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 5

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 6

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 7

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 8

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 9

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 10

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 11

By default, timestamps include microseconds (six digits to represent fractional seconds):

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 12

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 13

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 14

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 15

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 16

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:

Programming in ILE RPG - Defining Standalone Variables & Assigning Initial Values to Data - Figure 17

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!

Jim Buck

Jim Buck's career in IT has spanned more than 35 years, primarily in the college education, manufacturing, and healthcare industries. Past president (13 years) of the Wisconsin Midrange Computer Professional Association, he has served on several teams developing IBM and COMMON certification tests. Jim has co-authored several IBM i textbooks with Bryan Meyers that are used by many companies and in colleges worldwide. Other accomplishments include: recipient of the 2007 IBM System i Innovation - Education Excellence Award, 2014 COMMON President's Award, and 2013/2016/2017 IBM Champion - Power Systems.


Jim is the president and founder of imPower Technologies, where he provides professional IBM i training and consulting services. He is active in the IBM i community, working to help companies train their employees in the latest IBM technologies and develop the next generation of IBM i professionals.


MC Press books written by Jim Buck available now on the MC Press Bookstore.

Control Language Programming for IBM i Control Language Programming for IBM i
Master the A-Z of CL, including features such as structured programming, file processing enhancements, and ILE.
List Price $79.95

Now On Sale

Mastering IBM i Mastering IBM i
Get the must-have guide to the tools and concepts needed to work with today's IBM i.
List Price $85.95

Now On Sale

Programming in ILE RPG Programming in ILE RPG
Get the definitive guide to the RPG programming language.
List Price $95.95

Now On Sale

Programming in RPG IV Programming in RPG IV
Understand the essentials of business programming using RPG IV.
List Price $79.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: