Read an Excerpt
Programming in ILE RPG
By Bryan Meyers, Jim Buck MC Press
Copyright © 2015 Bryan Meyers and Jim Buck
All rights reserved.
ISBN: 978-1-58347-753-3
CHAPTER 1
Introduction to Programming and RPG
1.1. Chapter Overview
This chapter introduces you to RPG and describes its history and evolution from punched cards to a modern business programming language. It also explains general programming and computer-related concepts that you need to know as you begin to program in ILE RPG.
1.2. Programming
Computer programming involves writing instructions for a computer that tell it how to process, or manipulate, data. No matter how complex a computer application is, its actions are ultimately directed by individual lines of code that operate on input information and generate a result. When you organize and group those individual instructions together, they typically represent a step-by-step process that results in a specific product, such as a sales report. It is a computer programmer's job to design, organize, write, and test those individual lines of code, creating a working computer program as the end result.
The computer is a binary device with electronic components that can depict only two states: on and off, or flow of current and no flow. To represent those states, computers internally store and manipulate instructions (and data) as patterns of bits, or binary digits, with values of 1 or 0. Programmers originally were forced to write computer instructions as strings of 1s and 0s, using machine language. Humans, however, do not function as well at this low representation level. Fortunately, advances in computer science soon led to the development of high-level languages (HLLs).
A high-level language allows a programmer to write computer instructions in a format and syntax that is more easily recognizable than long strings of 1s and 0s. This HLL source code is stored in a file on the computer. But before the computer can actually execute the instructions, they must be translated into the bit patterns that the computer can recognize. The computer itself can accomplish this translation by using a special program called a compiler. A compiler reads the source code and translates it into machine language that the computer can understand.
1.3. History of RPG
IBM introduced the Report Program Generator (RPG) programming language in the early 1960s. RPG was one of the first HLLs. It filled a niche for providing quick solutions to a common business task: generating reports needed within the business. RPG was unique among computer languages in several respects.
RPG is a procedural language. Procedural languages typically require that you explicitly code each processing step or instruction, in the correct sequence, to define the procedure or process necessary for the computer to produce the end result. Unlike other procedural languages, RPG did not require the programmer to detail each processing step. Instead, the language included a fixed-logic cycle that automatically executed the normal read-calculate-write process in most report programs.
The RPG programmer's job was to accurately describe the files, record layouts, calculations, and output desired for a specific program. RPG required that these descriptive specifications appear in a specific sequence within a program and that entries within each specification line appear in fixed locations, or columns. The programmer typically coded these specifications onto paper hole-punch cards, a deck of which formed the source code for a program. The RPG compiler read that card deck and, through its logic cycle, supplied the needed missing steps to provide a standard machine language program for the computer to execute.
Another unique characteristic of RPG was its use of a special class of built-in variables called indicators. These variables, many of which simply had numbers for names, were predefined to the computer and could have only one of two values: '1' or '0' (corresponding to on or off). A programmer could set the indicators to on or off in one part of the program and then reference their status in another part of the program to specify what processing was to occur.
1.3.1. RPG II
By the late 1960s, RPG had gained popularity, especially in small and midsize data processing departments. Programmers were stretching the language beyond its original intended use, using RPG for complex computations and complicated file updating as well as for report generation.
Accordingly, IBM introduced an enhanced version of the language, RPG II, when it released its System/3 computer. A few other computer vendors observed the popularity of RPG and developed RPG II compilers for their minicomputers, but for the most part, RPG remained a language associated with IBM installations.
1.3.2. RPG III
During the 1970s, several trends in data processing became apparent. First, as computers became less expensive and more powerful, and as operating systems became more sophisticated, interest in interactive programs began to mushroom. In interactive applications, a user interacts directly with the computer through a terminal or workstation to control the actions of a computer program as it is running. Previously, programs had involved only batch processing, in which the computer processes a batch of data (typically representing business transactions) without user intervention.
A second emerging trend was a growing interest in a database approach to data management. With a database approach, programmers define data independently of programs by using a database design tool, such as Structured Query Language (SQL). The files storing the data are rigorously designed and organized to minimize redundancy and to facilitate accessing data stored in separate files. Any program can use these database files without having to explicitly define the details of the data within the program itself.
Finally, a third trend during that decade was an increasing concern with program design. This trend resulted in a methodology called structured design. As companies' libraries of developed programs continued to grow, the need to revise those programs to fit evolving business requirements grew as well. It became apparent that computer professionals had paid too little attention to the initial design of programs. Poorly designed programs were causing inefficiencies in program maintenance. Experts attributed much of this inefficiency to spaghetti code (i.e., programs that included undisciplined, haphazard transfer of control from one part of a program to another).
Advocates of structured design recommended restricting indiscriminate flow of control within a program and using only those operations that kept tight controls on that flow. With this emphasis on structured design, concepts of modular programming and code reusability also began to emerge.
IBM addressed all these trends when it introduced the System/38 minicomputer in 1979. This computer's architecture was unique in that the design of the computer and its operating system featured a built-in database. The S/38 required data files to be predefined at a system level before a program could reference or use those files. This requirement alone forced IBM to release a new version of RPG to allow external file definition. IBM called this version RPG III.
At this time, IBM also made several other major changes to RPG. First, it added features that made it easier for programmers to develop interactive applications. Second, to address structured design issues, IBM included structured operations for looping and decision logic. Finally, to support modular code and reusability, IBM revamped the language to include the capability to perform calls to other programs and to pass data between programs.
1.3.3. RPG/400
In 1988, IBM announced its successor computer to the S/38: the Application System/400, or AS/400. With the new computer came a new version of RPG — RPG/400.
Despite its name, RPG/400 was really just a compiler that read the existing RPG III syntax, with a few new operations and enhancements. Following RPG/400's initial release, IBM periodically added more features to the language, but these changes were relatively minor as well.
1.3.4. ILE RPG
Meanwhile, a growing number of critics accused RPG of being difficult to understand because of its short data names, abbreviated operation codes, and rigidly fixed format. The critics contended that the language was showing its age in its limited choice of data types (e.g., no direct support for date data types), its inability to handle multidimensional arrays, and its patchwork approach to data definition.
To address some of these criticisms, in 1994, concurrent with the release of Version 3 of the AS/400's operating system (called OS/400), IBM introduced a version of RPG sufficiently unlike earlier versions that it warranted a name change: ILE RPG, informally known as RPG IV. In addition to trying to address the criticisms previously mentioned, IBM included RPG as part of its newly introduced Integrated Language Environment (ILE), a programming model that allows program modules to be first compiled and then bound together into executable programs. This change supported the growing interest in developing reusable units of code and improving system performance. Moreover, it let programmers develop a program by using modules written in different languages and then bind these modules into a single program.
ILE RPG relaxes many of the strict fixed-format requirements previous RPG versions imposed, allowing free-format expressions and keyword notation in its specifications. Data-naming limits have been extended, and many other artificial limits have been effectively removed from the language. In addition, ILE RPG adds several new organizational constructs, including a central data-definition specification and procedure prototyping, which lets many program modules efficiently share information with each other. ILE RPG borrows many of the best features of other programming languages, incorporating those features into its own new syntax.
IBM has rebranded the AS/400 hardware several times over its lifetime to conform to current marketing strategies. It has been known by various combinations of AS/400, eServer, iSeries, and System i. Recently, IBM has consolidated its enterprise hardware into the Power Systems server line. The OS/400 operating system software has also been rebranded, first as i5/OS and now IBM i, which is the name we use in this text. Despite the system's various names, though, the ILE RPG syntax remains intact.
Recent releases of ILE RPG have focused on enabling the RPG architecture to coexist with Internet-based applications and objected-oriented languages, including Java. Modern e-business applications usually incorporate several hardware platforms (most notably, Intel-based computers) and software standards. As RPG evolves, it strives to maintain compatibility with these platforms and standards.
These changes have quieted, but not suppressed, RPG's critics. However, given the large base of existing RPG applications and IBM's present willingness to support RPG, it is likely that the language will continue to evolve and will remain the primary language for application development on IBM i for many years to come.
Comparing RPG programs written 20 years ago with those written by RPG professionals today reveals their great design differences. These differences are not due solely to the use of operations unavailable in the past, although the new operations enabled the changes. The biggest change is that RPG, originally a language that emphasized specification instead of procedure, has been transformed by programming practices into a largely free-format procedural language. Today's programmers virtually ignore RPG's fixed-logic cycle — the feature that made the language unique in the 1960s. And most modern programmers use RPG's indicators only in those instances in which the language absolutely requires their use.
1.3.5. Learning the RPG Language
Many RPG texts start by instructing students in RPG II and introduce RPG III or ILE RPG only after thoroughly indoctrinating the students in the fixed-logic cycle and the use of indicators. This book begins by teaching RPG as today's programmers use it.
The newest releases of the ILE RPG compiler allow free-format specifications, which provide many advantages, including better readability, improved reliability, the ability to indent code, and a similarity to other languages you may already know. Generally, this book presents free-format illustrations. Because free format is a relatively new feature of RPG, however, it's likely that you will encounter many older fixed-format RPG programs. To support those programs, we also include examples of fixed-format ILE RPG entries and compare them with their preferred free-format alternatives.
Now that you have an understanding of RPG's evolution, we can turn to some basic programming concepts that you need to know before you begin to learn ILE RPG programming.
1.4. Program Variables
Computer programs would be of little worth if you needed a different program each time you wanted to change the values of the data to be processed. For example, assume you are developing a payroll program and one processing step is to multiply the number of hours worked by pay rate. If you must rewrite this step to explicitly state the number of hours worked and the hourly pay rate for each employee, you are better off calculating wages manually or with a calculator. The power and value of computer programming rests in the concept of variables.
A program variable is a named data item within a program that represents a location in the computer's memory that can store data. When a programming instruction involves the manipulation of a variable, the computer checks the value stored at that memory location and uses that value in the calculation. Thus, you can instruct the computer to multiply the value stored in variable Hours by the value stored in variable Rate and then store the answer in variable GrossPay. If Hours contains 35 and Rate 6, GrossPay becomes 210; if Hours contains 40 and Rate 5, GrossPay becomes 200; and so on. In a program, you might represent this process by using variable names in this expression:
GrossPay = Hours * Rate;
RPG has traditionally used the term field rather than variable. Modern usage tends to restrict the term field to those variables defined within a database file and uses variable for other data items defined in the program, but the terms are generally interchangeable. RPG is a strictly typed language that requires you to define all variables by naming them, assigning each one a fixed length that determines the amount of memory allocated for storing the variable's values, and declaring what type of data (e.g., character, numeric, date) the variable contains. A program variable's name has meaning only within the context of the program where it is defined, even if it is a field in a database file. Subsequent chapters of this book examine the methods RPG uses to define variables and the data types it allows.
1.5. Libraries, Objects, and Data Files
An important IBM i architectural concept is that of objects. Almost every named entity is an object, and every object is categorized by type. The object type identifies the purpose of the object and how it is used by the computer. The following are some common object types:
Libraries (designated as object type *LIB), which are containers for other objects
Files (*FILE), which hold formatted data records
(Continues...)
Excerpted from Programming in ILE RPG by Bryan Meyers, Jim Buck. Copyright © 2015 Bryan Meyers and Jim Buck. Excerpted by permission of MC Press.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.