Reading IBM documentation I become a little bit confused about how to correctly write a program/procedure entry point.
Especially reading this https://publib.boulder.ibm.com/iseries/v5r2/ic2924/books/c092508410.htm
I see:
The prototype with the matching name must precede the procedure interface in the source
And here http://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/rzasd/freeinterface.htm
I read:
If you do not specify a prototype for a cycle-main procedure, you use *N as the name for the procedure interface
So I code a new program this way:
CTL-OPT ...
DCL-PI *N;
P1 CHAR(2);
P2 CHAR(2) CONST;
P3 CHAR(2) CONST;
P4 CHAR(1) CONST;
P5 PACKED(7) CONST;
P6 POINTER;
P7 INT(5);
END-PI;
...
and it compile and run fine.
So what I'm asking is, when and why should I specify a prototype before the interface?
The short answer to your question is always create a prototype for an RPG4 program. It may be optional, but you might need it down the road.
I have begun using linear main procedures for programs rather than cycle main. You do this by including ctl-opt Main(procname)
at the head of the program. I also put my prototypes in separate source files. That way I can easily include them with /copy
directives which has a side effect of collecting external program and service program dependencies near the top of my programs. I prefer to put all prototypes in a separate source file, named the same as the program source. Or for service programs, I name it the same as the service program. But some places I have worked will put a suffix at the end of the prototype and store it in the same service program with the program or module source. The nice thing at v7.1 is that you don't have to define prototypes for internal procedures, just the ones that are exported. At the program level, that is the main procedure, and while that is strictly not required, I always create that prototype in it's own source, because you never really know if you are going to want to call that program from somewhere else at some time in the future. Also while it is not required for you to include the prototype in the program itself, doing so is a good way to verify that the prototype is valid.