Thu, May
1 New Articles

V5R1 Enhancements to RPG IV

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

We in the RPG compiler development team at IBM think that V5R1 is the most significant release of RPG IV since V3R6/V3R2, when prototyped calls and subprocedures were introduced. The RPG IV enhancements in V5R1 fall into two main categories: functionality that makes it easier to write, read, and maintain your RPG code; and new functionality.

Totally Free-form Calculations

Perhaps the most noticeable enhancement to RPG is the addition of free-form calculations. Figure 1 shows a small example of the new free-form calculations in RPG IV. They start with /free and end with /end-free.

Free-form statements begin with the op code, are followed by one or more of factor 1, factor 2, and the result field, and end with a semicolon (;). You can code a statement on more than one line if you like, but you can only code one statement per line. You can leave out the EVAL and CALLP op codes if you want, unless you have operational extenders such as EVAL(H).

If a procedure call has no parameters, you must code empty parentheses as a parameter list. In traditional calculations, you may code the empty parentheses or not, however you choose.

In a free-form calculation, you begin comments with //. The remainder of the line is a comment. Line 11 of Figure 1 is a full-line comment, while line 18 has an end-of-line comment. You can also use // for full-line comments in traditional specifications, but endof-line comments are not allowed.

Not all the traditional fixed-form op codes are allowed in free-form calculations. However, several new built-in functions allow you to do most of your coding in free-form calculations. Figure 2 (page 78) shows a table of op codes that are not supported in free- form calculations, with the suggested replacement coding for free-form calculations. You can use SEU to code your free-form calculations, but no syntax checking will be done.

Data Structure Enhancements

The new QUALIFIED keyword for data structures allows you to name your subfields without worrying whether the name has already been used in the program. When you specify a subfield of a qualified data structure in calculations, you qualify the subfield name with the owning data structure name. For example, the data structure attrs has a subfield age. To use this subfield in calculations, you code attrs.age.

D age 10I 0
D height 4F
D weight 4F

EVAL attrs.age = 10

Since the subfields cannot be used alone, you are free to use the same names again within the same source member. You could reuse the name as a subfield in another data structure or as an ordinary item such as a file, standalone field, or subroutine. If two data structures have subfields with the same name, the subfields are completely independent; so the definitions of the subfields do not have to match at all. When qualified subfield names are continued across two lines, you can break the name after the dot, and avoid using the usual three dots:

D today 11P 3
D min 11P 3

D name 100A VARYING
D price 15P 3

D today S D DATFMT(*ISO)

Another new D-spec keyword, LIKEDS, says to define one data structure with the same subfields as another data structure. The new data structure is automatically a qualified data structure:

D myAttrs PR LIKEDS(attrs)

EVAL myAttrs.height = 168.9

Using LIKEDS, you can easily define several data structures alike:

D norm DS LIKEDS(attrs)
D cand DS LIKEDS(attrs)

if %abs(cand.age - norm.age) > 25;

sendMsg ('Abnormal age'

: candidate.age);


What makes LIKEDS very powerful is that you can use it for prototyped procedures on both the return value and the parameters. This gives you the ability to receive a data structure as a parameter:

P chkAttrs B
D chkAttrs PI
D at LIKEDS(attrs)

C at.height > MIN_HEIGHT
C at.weight < MAX_WEIGHT
P chkAttrs E

You can’t initialize individual subfields of a data structure defined with LIKEDS, but you can initialize the new data structure the same as the parent data structure using INZ(*LIKEDS).

More Granular Exception Handling

The MONITOR group has been added to RPG to allow for more localized and more general exception handling. The MONITOR group is very similar in concept to the CL MONMSG, except that, with the MONITOR group, you can monitor as many statements as you want.

The MONITOR group starts with the MONITOR op code and ends with the ENDMON op code. The first part of the MONITOR group is the MONITOR block, which contains the statements being monitored for errors. One or more ON-ERROR blocks follow the MONITOR block. These start with an ON-ERROR op code listing the status codes for the errors you want that ON-ERROR block to handle. The rest of the ON- ERROR block contains the code to handle those errors. You can code as many lines in each ON-ERROR block as you like.

You can code three special values in the ON-ERROR list:

• *FILE—Errors dealing with files
• *PGM—Program errors
• *ALL—All errors

You can nest MONITOR groups with other structured groups, including other MONITOR groups. If an error occurs in an inner MONITOR group that is not handled by that monitor, the error can be handled by the outer group.

If you call a subroutine from a MONITOR block, or if you do an input or output operation that uses I- or O-specs, any errors in the subroutine or I/O specs will be handled by the monitor. Of course, the subroutine could have its own MONITOR group to handle its own errors; in that case, the MONITOR group containing the EXSR would only handle the errors not handled by the subroutine’s monitor.

Figure 3 (page 79) shows an example of a MONITOR group. The blue section is the code that is being monitored. The first ON-ERROR block handles open errors; the second ON-ERROR block handles several errors that might occur handling the data; the last one handles all other errors.

More Control over File Opens

The new F-spec keywords EXTFILE and EXTMBR allow you to specify the file to be opened. You can use either literals or variables as the parameters to these keywords. Using a variable for either of the keywords allows you to wait until runtime to decide on the actual file without using an override command.

The EXTFILE parameter must be in the form ‘LIBRARY/FILE’; the library can be ‘*LIBL’. For EXTMBR, the name can be ‘*FIRST’ or ‘*ALL’.

Figure 4 has an example of a program using EXTFILE and EXTMBR. This program is the command processing program for a command that has three parameters: a file name, a member name, and an attribute character. The program sets all comment lines to the specified attribute. You can download full source code for the command and the RPG program at www.midrangecomputing.com/mc

Easy Interaction with Java

A new form of the EXTPROC keyword makes it very easy for RPG programmers to call Java methods and write Java native methods—Java methods that are actually procedures written in another language. In the EXTPROC keyword, you say that you are prototyping a Java method, and you give its class. You can specify that the method is static by using the STATIC keyword.

When you call an instance method (a nonstatic method), you specify the object instance by coding an additional first parameter. In Java, you might code string. getBytes(); in RPG, you would code this getBytes(string): If you want Java to call your RPG procedure, you tell Java that your procedure is a native method, and you tell RPG that your procedure is a Java method.

class MyClass

{native int trimLen (byte[] string);}

DtrimLen PR 10I 0 extproc(*JAVA
D :'MyClass'
D :'trimLen')
D string 100A varying const
Then you code your subprocedure normally.

P trimLen B export
D trimLen PI 10I 0
D string 100A varying const

C RETURN %len(%trim(string))


These are the big changes to RPG. There are several more small enhancements that you can find out about by reading the “What’s New in This Release” section of the V5R1

DgetBytes PR 65535A extproc(*JAVA
D :'java.lang.String'
D :'getBytes')
D varying

You can declare Java objects to be used with Java methods. You use the new CLASS keyword.

Dstring S O class(*JAVA
D :'java.lang.String')

eval data = getBytes(string)

RPG manuals. You can find the manuals at http://publib.boulder.ibm.com/pubs/ html/as400/infocenter.htm.


• iSeries 400 Information Center: http://publib.boulder. ibm.com/pubs/html/ as400/infocenter.htm


callp(e) filePrep(filename);

if %error;

cleanup('Error handling file' + filename);



open custFile;

setll klist custRec;

reade klist custRec;

dow not %eof(custFile);

// correct the area code

if areaCode = OLD_AREA_CODE

and custCity <> *blanks;

custAreaCode = getNewAreaCode(custCity);


reade klist custRec;


cleanup(); // normal cleanup

Figure 1: Free-form RPG IV calculations are a departure from traditional RPG coding.

Opcode Suggested free-form replacement (if any)
ADD + operator
ADDDUR "+ operator with new duration BIFs %YEARS, %DAYS, %MINUTES, etc."
ANDxx AND operator
BITOFF, BITON No replacement
CABxx No replacement
CASxx SELECT group with EXSR
CAT + operator
COMP IF opcode
DEFINE Definition specifications
DIV / operator or %DIV
DO FOR opcode
DOUxx, DOWxx DOU and DOW opcodes

GOTO No replacement
IFxx IF opcode
LOOKUP New BIFs %LOOKUP, %LOOKUPxx (arrays) and %TLOOKUP, %TLOOKUPxx (tables)
MULT * operator
Opcode Suggested free-form replacement (if any)

MxxZO No replacement
ORxx OR operator
PARM Definition specifications
PLIST Definition specifications
SUB + operator
SUBDUR - operator with new duration BIFs %YEARS, %DAYS, %MINUTES
TAG No replacement
TESTB No replacement
TESTN No replacement
TESTZ No replacement
WHENxx WHEN opcode
Note: Some of the opcodes that have no free-form replacement, such as GOTO and TAG, will not likely ever
have free-form replacements. Others, such as BITON and BITOFF will likely have free-form replacements in the

Figure 2: Many op codes are not supported in free-form calculations.



open file;

read file;

dow not %eof;

. . . handle the record


ON-ERROR 1216;

error = 'File not found';

ON-ERROR 113 : 102: 103;

error = 'Error with input data';

addBadRrn (rrn);


error = 'Unexpected error ' +




Figure 3: MONITOR groups provide a better way to trap errors.

F EXTFILE(fileName)
F EXTMBR(memberName)

D fileName S 21A

// a type-definition for a library-qualified name
D name 10A
D lib 10A // the prototype for this program
D pgm PR
D filelib LIKEDS(qualName) CONST
D mbr 10A CONST
D attribute 1A CONST // the procedure interface for this program (entry parameter list)
D pgm PI
D filelib LIKEDS(qualName) CONST
D memberName 10A CONST
D attribute 1A CONST


fileName = %trim(filelib.lib) + ‘/’ + filelib.name;

open qrpglesrc;

Figure 4: The EXTFILE and EXTMBR keywords let you specify a file’s name at runtime.



Support MC Press Online

$0.00 Raised:

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: