12
Thu, Dec
4 New Articles

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!

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • 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.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • 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

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • 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: