Skip to content

Procedure Overloading

danlong005 edited this page Aug 30, 2026 · 2 revisions

Procedure Overloading

OVERLOAD declares a generic procedure name that dispatches to one of several typed implementations based on argument types at call time.

Declaration

// Typed implementations
DCL-PR absInt INT(10);
  n INT(10) VALUE;
END-PR;

DCL-PR absFloat FLOAT(8);
  n FLOAT(8) VALUE;
END-PR;

// Generic overloaded name
DCL-PR abs OVERLOAD(absInt : absFloat);
END-PR;

Calling

The compiler selects the implementation whose parameter types best match the argument types at the call site:

DCL-S i INT(10);
DCL-S f FLOAT(8);

i = abs(-7);      // calls absInt
f = abs(-3.5);    // calls absFloat

Full Example

**FREE

CTL-OPT MAIN(main);

DCL-PR formatInt   VARCHAR(30);
  n INT(10) VALUE;
END-PR;

DCL-PR formatFloat VARCHAR(30);
  n FLOAT(8) VALUE;
END-PR;

DCL-PR format OVERLOAD(formatInt : formatFloat);
END-PR;

DCL-PROC main;
  DCL-PI main; END-PI;

  DSPLY format(42);      // calls formatInt  → "INT:42"
  DSPLY format(3.14);    // calls formatFloat → "FLT:3"
END-PROC;

DCL-PROC formatInt EXPORT;
  DCL-PI formatInt VARCHAR(30);
    n INT(10) VALUE;
  END-PI;
  RETURN 'INT:' + %CHAR(n);
END-PROC;

DCL-PROC formatFloat EXPORT;
  DCL-PI formatFloat VARCHAR(30);
    n FLOAT(8) VALUE;
  END-PI;
  RETURN 'FLT:' + %CHAR(%INT(n));
END-PROC;

Clone this wiki locally