A prototype may be explicitly or implicitly defined. If the procedure is called from a different RPG module, the prototype must be explicitly specified in both the calling module and the module that defines the procedure. If the procedure is only called within the same module, the prototype may be explicitly defined, or it may be omitted. If the prototype is omitted, the compiler will implicitly define it from the procedure interface.
For modules that call a procedure that is defined in a different module, a prototype must be included in the definition specifications of the program or procedure that makes the call. The prototype is used by the compiler to call the program or procedure correctly, and to ensure that the caller passes the correct parameters.
A prototype must have a name. If the keyword
EXTPGM or EXTPROC is specified on the prototype definition, then any
calls to the program or procedure use the external name specified
for that keyword. If neither keyword is specified, then the external
name is the prototype name in uppercase.
In free form, specify the DCL-PR operation code followed
by the prototype name and keywords;
in fixed form, specify PR in the Definition-Type entry (positions 24-25). Any
parameter definitions must immediately follow the PR specification.
In free-form, the prototype definition ends with the END-PR
statement; in fixed form, the prototype definition ends with the first definition specification
with non-blanks in positions 24-25 or by a non-definition specification.
For information on these keywords, see Definition-Specification Keywords. Figure 1 shows a prototype for a subprocedure CVTCHR that takes a numeric input parameter and returns a character string. Note that there is no name associated with the return value. For this reason, you cannot display its contents when debugging the program.
* The returned value is the character representation of
* the input parameter NUM, left-justified and padded on
* the right with blanks.
D CVTCHR PR 31A
D NUM 31P 0 VALUE
* The following expression shows a call to CVTCHR. If
* variable rrn has the value 431, then after this EVAL,
* variable msg would have the value
* 'Record 431 was not found.'
C EVAL msg = 'Record '
C + %TRIMR(CVTCHR(RRN))
C + ' was not found '
If you are writing a prototype for an exported subprocedure or for a main procedure, put the prototype in a /COPY file and copy the prototype into the source file for both the callers and the module that defines the procedure. This coding technique provides maximum parameter-checking benefits for both the callers and the procedure itself, since they all use the same prototype.