You are on page 1of 7

Subprocedure Basics | IBM i | IBM Systems Magazine

1 of 7

http://www.ibmsystemsmag.com/ibmi/enewsletterexclusive/26220p2.aspx

Administrator
Backup and Recovery
DB2
High Availability
LPAR
Networks
Performance
Security
Systems Management
Tivoli
Trends
AIX
Linux
Open Source
What's New
Tips & Techniques
Application Development
Systems Management
Miscellaneous
Case Studies
Automotive
Healthcare
Manufacturing
Miscellaneous
Non-profit
Retail
Storage
Disk
Servers
Software
Tape
Product News
Buyer's Guide
Administrator
Backup and Recovery
DB2
Domino
High Availability
LPAR
Networks
Performance
Printing
Security
Systems Management
WebSphere
Windows Integration

Trends
IBM Announcements
Linux
Open Source
SOA
What's New
Tips & Techniques
Application Development

9/27/2010 8:53 AM

Subprocedure Basics | IBM i | IBM Systems Magazine

2 of 7

http://www.ibmsystemsmag.com/ibmi/enewsletterexclusive/26220p2.aspx

Systems Management
Case Studies
Automotive
Banking/Finance
Healthcare
Insurance
Manufacturing
Miscellaneous
Non-profit
Retail
Storage
Disk
Optical
Servers
Tape
Product News
Product Reviews
ENDPGM Main Page
Administrator
Backup and Recovery
CICS
DB2
High Availability
IMS
LPAR
Migration
Networks
Performance
Security
Systems Management
Tivoli
Trends
Linux
Open Source
Security
SOA
What's New
z/OS
z/VM
Tips & Techniques
Application Development
Systems Management
Case Studies
Automotive
Banking/Finance
Healthcare
Insurance
Manufacturing
Miscellaneous
Retail
Storage
Disk
Servers
Software
Tape
Product News
Stop Run
Buyer's Guide Main Page

9/27/2010 8:53 AM

Subprocedure Basics | IBM i | IBM Systems Magazine

3 of 7

http://www.ibmsystemsmag.com/ibmi/enewsletterexclusive/26220p2.aspx

Business Strategy
Competitive Advantage
Consolidation
Executive Perspective
Green IT
Migration
Open Source
ROI
Infrastructure
Blades
Storage
Systems Management
Case Studies
Distribution
Healthcare
Manufacturing
Services
Web 2.0
Cloud
Social Media
Trends
Collaboration
IBM Announcements
IBM Research
Open Source
Social Media
What's New
Product News

AIX
MAINFRAME
POWER
Newsletters
About Us

Subscribe
Current Issue
Archive

IBM i
ALL EDITIONS
ADMINISTRATOR
DEVELOPER
TRENDS
TIPS & TECHNIQUES
CASE STUDIES

9/27/2010 8:53 AM

Subprocedure Basics | IBM i | IBM Systems Magazine

4 of 7

http://www.ibmsystemsmag.com/ibmi/enewsletterexclusive/26220p2.aspx

STORAGE
PRODUCT NEWS
ENDPGM
BUYER'S GUIDE

Developer > RPG

Subprocedure Basics
How and why to convert subroutines to subprocedures
August 2009 | by Susan Gantner and Jon Paris

Print

Email

Our next step requires a bit of thought about how the existing subroutine works. In our case, the subroutine's purpose is to determine the day of the
week associated with a given date value. So its caller must provide that date. In our example, the field called WorkDate is used as the input for the
subroutine. The logic then calculates the day of the week and the result of the calculation ends up in the WorkDay field. In this example, the
WorkDay field will contain a value of 1 for a Monday, 2 for Tuesday, etc.
Consequently, when turning this logic into a subprocedure, we'll need to accept a date value as an input parameter from the main program and the
subprocedure can return the numeric day number as output. While we could write this subprocedure to accept two parameters (one for the input date
value and one for the output numeric value), it's a better idea to code the subprocedure as a function that returns a valuemuch like RPG built-in
functionsas this makes it more flexible. So we'll write our subprocedure to accept one input parameterthe dateand itll return the day number
to the caller. As a result, invoking our subprocedure will look much like a built-in function call:

Although subprocedures use parameters, they dont use the old-fashioned *ENTRY PLIST approach. Rather the parameter list for our subprocedure
is coded on D specs in a structure called a Procedure Interface (PI). The PI for our DayOfWeek procedure will look like this:

The first line identifies the type of return value that our subprocedure provides. In our example, this is 1S 0 which indicates that it will return a
one-digit numeric value. The second (and subsequent) lines of the PI define the parameters that are passed in. In our example, we have only one
parameter, the WorkDate field. We couldve optionally included the name of the procedure on the first line of the PI. However, since the PI will be
immediately following the beginning P spec for the procedure, it seems a bit redundant. Note that the columns directly under the "PI" are blank to
indicate that WorkDate is part of the PI (i.e., its a parameter). Syntactically, the PI is much like a DS in that it continues until something else is coded
in that column. So if we had multiple parameters being passed into the subprocedure, wed simply code them on subsequent D-specs after the
WorkDate definition.
After the PI is completed, we can code the definitions of any local fields that the subprocedure will use. We covered some of the advantages of local
data last month, so we'll refrain from expounding on that again. Any fields used in the subprocedure should have their definitions moved from the
global D specs at the top of the source member into the D specs inside the subprocedure. By definition, any fields defined inside the subprocedure like
this become local to the procedure and cant be seen or used in any other part of the program.
In our original program, we had four fields (AnySunday, WorkNum, WorkDate and WorkDay) that were defined primarily for use in the DayOfWeek
subroutine. We've already defined WorkDate in the PI. So that leaves three fields whose definitions should be moved from the global D specs at the
top of the source member to the local D specs. So the first part of our subprocedure code now looks like the following. Note the "S" for the standalone
field WorkNum also effectively marks the end of the PI (i.e., the end of the parameter list).

9/27/2010 8:53 AM

Subprocedure Basics | IBM i | IBM Systems Magazine

5 of 7

http://www.ibmsystemsmag.com/ibmi/enewsletterexclusive/26220p2.aspx

Some may wonder how we can remove the definitions for WorkDate and WorkDay from the global D-specs since they were referenced in the original
program's main line. For the answer, let's revisit the way we called our subroutine compared with the way were now calling our subprocedure.
Our original program code called the subroutine like this:

If this subroutine were only called from one place in the program, perhaps we wouldn't have needed the statements to move InputDate to WorkDate
and WorkDay to DayNumber. But often such subroutines are called from more than one point in the code, and this kind of data movement is
necessary to set up the specific data that will be operated on.
Next page: >>
Page 1 2 3
Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.
More Articles From Susan Gantner
Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.
More Articles From Jon Paris

Advertisement

WEBINAR
Thursday, September 30 2pm (ET) from LANSA
IBM i Enterprise Web Development ---- Fact or Fiction?

Browse products and services for Developer.

Advertisement

9/27/2010 8:53 AM

Subprocedure Basics | IBM i | IBM Systems Magazine

6 of 7

http://www.ibmsystemsmag.com/ibmi/enewsletterexclusive/26220p2.aspx

Maximize your IT investment with monthly information from THE source...IBM Systems Magazine EXTRA & Marketplace eNewsletters.
SUBSCRIBE NOW.
View past IBM i EXTRAs here

Related Articles
V5R4 Introduces Subroutines for CL
Administrator
The Case for CODE/400
E-Newsletter Exclusive
Paging RPG IV
RPG for the Web
Three Good Reasons to Stop Writing Subroutines
E-Newsletter Exclusive | Save time and reduce errors with subprocedures

9/27/2010 8:53 AM

Subprocedure Basics | IBM i | IBM Systems Magazine

7 of 7

http://www.ibmsystemsmag.com/ibmi/enewsletterexclusive/26220p2.aspx

IBM i
AIX
MAINFRAME
POWER
Homepage
About Us
Contact Us
Subscriptions
Editorial Calendar
Advertise With Us
Reprints
Privacy Policy
Terms of Service
Sitemap
IBM Systems Magazine is a trademark of International Business Machines Corporation. The editorial content of IBM Systems Magazine is placed on
this website by MSP TechMedia under license from International Business Machines Corporation.
2010 MSP Communications, Inc. All rights reserved

9/27/2010 8:53 AM

You might also like