๐ŸŽ‰ Active offers RPG10 First lesson is FREE! โ€ข RPG25 25% off the full series โ€ข
ILERPG.com โ€” IBM i ยท ILE RPG ยท AS400 Mastery
2 min readยท Admin

Copy Members in ILE RPG: One Definition, Everywhere It Is Used

A procedure called from ten programs needs ten matching prototypes. Typed by hand, they drift. A copy member keeps one definition and brings it into every compile that needs it.

/COPY and /INCLUDE

Both directives insert the contents of a source member or an IFS stream file at that point in the compile. /copy QPROTOSRC,ITEMPR finds the source file through the library list; a quoted path reads from the IFS. The RPG compiler treats the two directives the same way. The SQL precompiler, by default, expands /COPY and not /INCLUDE โ€” worth knowing before a host variable defined in an included member goes missing.

Guard every member

A member copied twice in one compile declares everything twice. Three conditional-compilation directives prevent that.

/if defined(ITEMPR_COPIED)
/eof
/endif
/define ITEMPR_COPIED

Copy the prototypes into the implementation too

When the module that defines a procedure copies the same prototype member as its callers, the compiler checks the procedure interface against the prototype. A change made on one side but not the other becomes a compile error in the module, not a runtime surprise in a caller.

What belongs in a copy member

Prototypes, TEMPLATE data structures, named constants, and shared record layouts โ€” such as a program-described file's positions โ€” all belong. Working storage does not: a counter in a copy member gives every program its own counter and hides where the state lives.

Conditional code

/IF DEFINED(TRACE) โ€ฆ /ENDIF includes code only when the compile specifies DEFINE(TRACE), which keeps diagnostic output out of production objects without editing the source.

Lesson 27's exercise examines IBM's own include members on PUB400, then builds a guarded copy member, a module, and a program bound from both.

Get the lesson

Lesson 27, โ€œCopy Members and Modular Source Organization,โ€ is part of Beginner ILE RPG in IBM i ILE RPG Mastery and is available at ILERPG.com for USD 10. The complete Beginner ILE RPG series โ€” all 18 lessons โ€” is USD 180, or USD 135 with coupon code RPG30, which takes 25% off when you purchase the full series.