Repository navigation
Procedure Overloading
Daniel Long edited this page Sep 24, 2026
·
2 revisions
OVERLOAD declares one procedure name that calls one of several candidate
procedures, chosen by the arguments at each call. rpgc follows IBM i's rules.
DCL-PR fmtNum VARCHAR(40);
n PACKED(15:2) VALUE;
END-PR;
DCL-PR fmtText VARCHAR(40);
s VARCHAR(30) CONST;
END-PR;
DCL-PR format VARCHAR(40) OVERLOAD(fmtNum : fmtText);- The overloaded prototype is one statement: it has no parameters, so no
END-PR(RNF3551). -
Every candidate returns the overloaded prototype's type — here
VARCHAR(40)(RNF3244).
A call goes to the one candidate its arguments fit:
- the number of arguments must be one the candidate takes (
OPTIONS(*NOPASS)makes trailing parameters optional); - a
VALUEorCONSTparameter takes any argument of its type family — a number for any numeric type, a string forCHARorVARCHAR, a date for aDATE; - a parameter passed by reference takes only a variable of exactly its type.
If no candidate fits, the call is an error (RNF3245); so is a call that more
than one fits (RNF3246). rpgc does not pick the closest match. An INT(10)
candidate and a FLOAT(8) one, both by VALUE, both fit abs(-7), so that
pair cannot be overloaded. Make candidates differ in type family or in number
of parameters instead.
format(42); // a number: fmtNum
format('hello'); // a string: fmtText**FREE
CTL-OPT MAIN(main);
DCL-PR fmtNum VARCHAR(40);
n PACKED(15:2) VALUE;
END-PR;
DCL-PR fmtDate VARCHAR(40);
d DATE VALUE;
END-PR;
DCL-PR fmtText VARCHAR(40);
s VARCHAR(30) CONST;
END-PR;
DCL-PR square INT(10);
side INT(10) VALUE;
END-PR;
DCL-PR rectangle INT(10);
width INT(10) VALUE;
height INT(10) VALUE;
END-PR;
// An overloaded prototype is one statement: no parameters, no END-PR
DCL-PR format VARCHAR(40) OVERLOAD(fmtNum : fmtDate : fmtText);
DCL-PR area INT(10) OVERLOAD(square : rectangle);
DCL-PROC main;
DCL-PI *N;
END-PI;
DCL-S count INT(10) INZ(42);
DCL-S due DATE;
due = %DATE('2024-01-15');
DSPLY format(count); // a number: fmtNum
DSPLY format(3.5); // any numeric type: fmtNum
DSPLY format(due); // a date: fmtDate
DSPLY format('hello'); // a string: fmtText
DSPLY %CHAR(area(4)); // one argument: square
DSPLY %CHAR(area(3 : 5)); // two: rectangle
END-PROC;
DCL-PROC fmtNum;
DCL-PI *N VARCHAR(40);
n PACKED(15:2) VALUE;
END-PI;
RETURN 'number ' + %CHAR(n);
END-PROC;
DCL-PROC fmtDate;
DCL-PI *N VARCHAR(40);
d DATE VALUE;
END-PI;
RETURN 'date ' + %CHAR(d);
END-PROC;
DCL-PROC fmtText;
DCL-PI *N VARCHAR(40);
s VARCHAR(30) CONST;
END-PI;
RETURN 'text ' + s;
END-PROC;
DCL-PROC square;
DCL-PI *N INT(10);
side INT(10) VALUE;
END-PI;
RETURN side * side;
END-PROC;
DCL-PROC rectangle;
DCL-PI *N INT(10);
width INT(10) VALUE;
height INT(10) VALUE;
END-PI;
RETURN width * height;
END-PROC;- Installation
- Quick Start
- CLI Reference
- Language Basics
- Data Types
- Variables and Constants
- Data Structures
- Arrays
- Varying-Dimension Arrays
- Control Flow
- Procedures
- Procedure Overloading
- Built-In Functions
- Error Handling
- Operation Extenders
- Enumerations
- Program Status Data Structure
- Data Areas
- SND-MSG
- Embedded SQL
- Record-Level Access
- Display Files (WORKSTN)
- Program-Described Files (I-Specs and O-Specs)
- DATA-INTO and DATA-GEN
- XML-INTO
- Fixed-Format Source
- Database Connections
- Multi-Module Programs
- Environment Variables
- Testing
- Complete Example: Employee Report
- IBM i Conformance Results
- IBM i Execution Results