Adding 'Value' to Your Prot...1
Transcript of Adding 'Value' to Your Prot...1
-
8/9/2019 Adding 'Value' to Your Prot...1
1/7
-
8/9/2019 Adding 'Value' to Your Prot...1
2/7
-
8/9/2019 Adding 'Value' to Your Prot...1
3/7
Business Strategy
Competitive Advantage
Consolidation
Executive Perspective
Green IT
Migration
Open Source
ROI
Infrastructure
Blades
StorageSystems Management
Case Studies
Distribution
Healthcare
Manufacturing
Services
Web 2.0
Cloud
Social Media
Trends
Collaboration
IBM AnnouncementsIBM 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
ng 'Value' to Your Prototypes | IBM i | IBM Systems Magazine http://www.ibmsystemsmag.com/ibmi/december04/spotlight/830
9/27/2010 1
-
8/9/2019 Adding 'Value' to Your Prot...1
4/7
STORAGE
PRODUCT NEWS
ENDPGM
BUYER'S GUIDE
Product News
Presented by:
ASG
Adding 'Value' to Your Prototypes
December 2004 | by Jon Parisand Susan Gantner
Print Email
Let's examine a simple C-function prototype that illustrates the use of the VALUE keyword and allows us to point out a few other C-function idiosyncrasies. The C
function to calculate the sine of a number, and in fact those for the whole of the math library, are simple and work well for this example despite having limited
applicability in most RPG applications. This is what the prototype for the sine function looks like when translated to RPG:
D Sine PR 8F ExtProc('sin')
D Double 8F Value
Note the use the EXTPROC keyword. We don't often use that keyword when calling RPG subprocedures, because the compiler supplies it by default. However, we
need to use it when calling C functions, because most are spelled in lower case. Since the RPG compiler upper cases every name that isn't in quotation marks, we
must use the EXTPROC keyword to ensure that the name of the function is correct ly identified as 'sin.' It also provides a good excuse to override the name to one
that's actually spelled correctly and makes more sense in our program logic. (After all, isn't there enough 'sin' in the world already?) If we hadn't specified
EXTPROC, the compiler would have translated the procedure name from 'Sine' to SINE.
You may also have noticed the use of the floating point (F) data type-something else we don't often use in RPG. The 8F definition corresponds to what C calls a
"double," meaning double-precision floating point. Don't be put of f by this. Although floating point is widely used in C functions, you won't have to worry about it or
define fields in floating-point format other than within prototypes. Why? For one thing, the VALUE keyword allows us to specify any numeric field, literal or function as
the input parameter . Under the covers, the compiler converts it to floating point. Similarly, although the functions return value is a lso specified as floating point, the
normal rules of RPG apply and the EVAL convert it to any format required, just as a packed-numeric return value can be assigned to a signed-numeric field. Its all
automatic.
Now that our prototype is in place, we can use the sine function just as easily as if IBM had added it to the RPG language. In fact, the only real difference is that if
IBM had added it, it would have been %SINE! A simple invocation of the function would look like this:
D X S 9p 2
D SineX S 7s 2
C Eval SineX = Sine(X)
Look-no floating-point fields.
Null-Terminated Strings
The C language doesn't deal well w ith the fixed-length character fields that are an RPG staple. Instead, if a character value is to be passed to a C function, its
typically expected as a null-terminated str ing-a variable-length character f ield that contains a null (X'00') denoting the end of the data. Actually, to be specific, C
typically expects that a pointer to a null-terminated str ing will be passed. To simplify coexistence with C functions, the compiler writers provided Options(*String),
which allows the compiler to do the tedious task of copying data to a temporary field, adding the X'00' to the end and passing the temporary fields address. To see
how this works, consider this example:
D Command S 40A Inz('command goes here') Varying
D CallSystem Pr 10I 0 ExtProc('system')
D CmdString * Value Options(*String)
D ExceptionMsg S 7A Import('_EXCP_MSGID')
C If CallSystem(Command) > 0
* Command failed
This is an example of using the C-system function. It serves a similar purpose to the QCMDEXC API. One of its advantages is that it returns a value that tells you
whether the command executed successfully. Because of this we can invoke the command as part of an IF statement, thereby executing the command and test ing
ng 'Value' to Your Prototypes | IBM i | IBM Systems Magazine http://www.ibmsystemsmag.com/ibmi/december04/spotlight/830
9/27/2010 1
-
8/9/2019 Adding 'Value' to Your Prot...1
5/7
the result in one step. If you want to know why the command failed, you can access the CPF message ID by importing the _EXCP_MSGID variable as shown
above.
Next page: >>
Page 12 3
Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.
More Articles From Jon Paris
Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.
More Articles From Susan Gantner
Advertisement
WEBINAR
Thursday, September 30 2pm (ET) from LANSA
IBM i Enterprise Web Development ---- Fact or Fiction?
Advertisement
ng 'Value' to Your Prototypes | IBM i | IBM Systems Magazine http://www.ibmsystemsmag.com/ibmi/december04/spotlight/830
9/27/2010 1
-
8/9/2019 Adding 'Value' to Your Prot...1
6/7
Maximize your IT investment with monthly information from THE source...IBM Systems MagazineEXTRA & Marketplace eNewsletters.
SUBSCRIBE NOW.
View past IBM i EXTRAs here
Related Articles
The Mystery of the Command Line Parameters
Developer
Data Orchestration
Administrator
iSeries EXTRA: Jon and Susan Answer Your Questions on Procedures
E-Newsletter Exclusive
Overcoming the Challenges of Embedding SQL into RPG Programs
Developer
ng 'Value' to Your Prototypes | IBM i | IBM Systems Magazine http://www.ibmsystemsmag.com/ibmi/december04/spotlight/830
9/27/2010 1
-
8/9/2019 Adding 'Value' to Your Prot...1
7/7
Passing Parameters in Scheduled Jobs
Administrator
IBM i
AIX
MAINFRAME
POWER
Homepage
About Us
Contact UsSubscriptions
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
ng 'Value' to Your Prototypes | IBM i | IBM Systems Magazine http://www.ibmsystemsmag.com/ibmi/december04/spotlight/830