Exporting procedure names is a bit confusing. By default, RPG IV procedure names are exported with the name specified in positions 7 to 21 of the Procedure specification (a.k.a. P specification or P-spec). But you can export a procedure name as any name you want, effectively creating an alias for the procedure name itself.
RPG IV is a case-insensitive language. Whether you write in uppercase, lowercase, or mixed case, it is all converted to uppercase by the compiler. For example, CustNo, custno, or CUSTNO would be converted to CUSTNO.
While this example is for field names, the same holds true for procedure names. However, the language does provide a way for you to export and call a procedure that is not in uppercase. Why is there a specific feature for non-uppercase procedure names? Consider this: RPG converts all procedure names, like it does for field names, to uppercase. But there are other languages that are case-sensitive. The C language, for example is case-sensitive. So procedures written in C are not converted to uppercase. Consequently, in order for RPG IV to be able to call a procedure written in C, it must have a mechanism to call non-uppercase procedure names.
A procedure in RPG IV consists of four parts:
- Beginning and ending P specifications--The beginning and ending P specifications outline the body of the procedure implementation as well as the procedure interface.
- Procedure interface--The procedure interface is the "entry plist" of the procedure. It must immediately follow the beginning P specification.
- Procedure implementation--The procedure implementation includes the Definition and Calculation specifications of the procedure that holds the variables, and it does the work of the procedure.
- Prototype--The prototype is a separate set of code that the compiler uses to make sure the parameters you pass to the procedure match those that are defined on the procedure interface. It is the prototype that also allows you to identify the name used to export (make available) the procedure name. That is, the prototype can be used to export the procedure name in uppercase, lowercase, or mixed case.
To illustrate these components, let's look at a simple example of a procedure. First, we have the P specifications. Our example procedure will calculate the cube root of a given value. The cube root will be returned to the caller.
The P specifications for the CUBEROOT procedure are as follows:
** Procedure implementation code goes here.
P CubeRoot E
By default, the RPG compiler converts this procedure name from "CubeRoot" as specified above to "CUBEROOT."
Next, we need to add the procedure interface. This is the parameter list for the procedure as well as the return value. The procedure interface is inserted after the beginning P specification as follows:
D CubeRoot PI 30P10
D nValue 30P10 Const
** Procedure implementation code goes here.
P CubeRoot E
Note that the name of the procedure is repeated on the procedure interface. While this is optional, I strongly recommend that you always specify the procedure name with your procedure interface.
The P specifications and the procedure interface are the three locations where the procedure name is specified when creating the implementation of the procedure.
In addition to the procedure interface, there is the prototype. As mentioned, the prototype is used by the compiler to syntax check the parameters passed to the procedure. It does this by testing the data types as well as checking whether or not they are constant (i.e., literal) values.
The easiest way to create a prototype is to copy the entire procedure interface and change the "PI" on the first line to "PR."
So this:
D nValue 30P10 Const
Becomes this:
D nValue 30P10 Const
The subtle difference is in columns 24 and 25: "PI" for the procedure interface and "PR" for the prototype.
It is a great programming practice to store the prototypes for your procedures in a secondary source member. For example, if the procedure is in a source member named MYSTUFF in QRPGLESRC, you might store the prototypes for the procedures you've created in MYSTUFF in a source member named MYSTUFF in the QCPYSRC source file. Don't have a QCPYSRC file? Create it as follows:
Upper/Lowercase Procedure Names
So how do you export a procedure name in upper/lowercase? In the procedure interface and prototype, the procedure name is specified on regular RPG IV source lines. Therefore, the procedure name is always converted to uppercase. So "CubeRoot" becomes "CUBEROOT" when compiled.
Procedure names are case-sensitive. If you attempt to call the CUBEROOT procedure in mixed case, the compile will fail. For example:
This will not compile because the procedure name is quoted on the CALLB opcode and the compiler does not convert quoted text strings to uppercase. The procedure name is CUBEROOT, not CubeRoot, so the compile will fail.
To cause a procedure name to be created with an upper/lowercase name, you must add the EXTPROC keyword to the prototype. Normally, this keyword is not needed, but when the procedure name you want to call is either different from the prototype name or not in uppercase, then you need to specify the EXTPROC keyword, as follows:
The EXTPROC keyword identifies the name of the procedure that the compile is going to reference when you do a call to the prototype. That's right: The call is still made to the name you specify on the prototype in columns 7 to 21 and is case-insensitive. But the compiler actually evokes the procedure name identified on the EXTPROC keyword. The complete CUBEROOT prototype follows:
D nValue 30P10 Const
When this is compiled, the name "CubeRoot" in columns 7 to 21 is folded to uppercase by the compiler and becomes "CUBEROOT." However, the actual procedure 'CubeRoot' specified on the EXTPROC keyword is not translated since it is quoted.
To call the 'CubeRoot' procedure, you still specify the prototype name (columns 7 to 21). The way it is specified (uppercase, lowercase, mixed case) is irrelevant. For example:
C eval nRoot = CUBEROOT(1234)
C eval nRoot = cuberoot(1234)
All three of the previous statements refer to the prototype name. Since they are not quoted character strings, RPG IV converts all three to uppercase, as follows:
C EVAL NROOT = CUBEROOT(1234)
C EVAL NROOT = CUBEROOT(1234)
The compiler also converts the prototype to uppercase, as follows:
D NVALUE 30P10 CONST
Note that since the EXTPROC keyword includes 'CubeRoot' (in quotes), the compiler avoids converting that string to uppercase. Consequently, the name 'CubeRoot' is used as the procedure name.
The prototype must be included in the same source member as that of the procedure implementation. The compiler uses the prototype to make sure it matches the parameters of the procedure interface and to identify the name used to export the procedure.
Exporting a Procedure Name
The EXPORT keyword allows a procedure to be called from another module (e.g., from another source member). The syntax to allow this is as follows:
The EXPORT keyword is specified on the beginning P specification. Without this keyword, you will not be able to call the procedure from outside the source member that contains its implementation.
As a rule of thumb, you should always specify EXPORT, avoiding it only when you are certain you will not need to call it from outside the source member. If you make it a habit, as I have, you can avoid those cryptic "Reference to undefined symbol..." messages.
Here's the entire CubeRoot procedure defined in a source member, along with a dummy example of how to call it. This source member should compile on all versions of OS/400 and i5/OS.
The Prototype Source Member
The Sample Procedure and Usage
H OPTION(*NODEBUGIO : *SRCSTMT)
/IF DEFINED(*CRTBNDRPG)
H DFTACTGRP(*NO) ACTGRP(*NEW)
/ENDIF
/COPY QCPYSRC,MYMATH
D nRoot S 20P10
C eval *INLR = *ON
C eval nRoot = CubeRoot(1234)
P CubeRoot B EXPORT
D CubeRoot PI 30P10
D nValue 30P10 Const
C return nValue ** (1/3)
P CubeRoot E
Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.
LATEST COMMENTS
MC Press Online