Learn how to use a prototyped call, an alternative to an ILE RPG sub-procedure, to call one program from another
Editor’s Note: This article is excerpted from chapter 8 of 21st Century RPG: /Free, ILE, and MVC, by David Shirey.
In some situations, when using an ILE sub-procedure in your program, you may want to write the sub-procedure up as a small program and then call it from another program. Granted, that is not technically a sub-procedure, but we will let that go for the moment and concentrate on the call of one program by another.
Back in the olden days, we would have used a CALL with a PARM list. But the regular, old CALL was not carried over into /Free. It wasn’t efficient enough. And so, the new way to call from one program to another is via the CALLP, a prototyped call.
What is a prototyped call? Well, as we have already seen, it is a structured call where the parameters are defined in both the calling and the called program. Fortunately, you have already had a look at the prototyping tools in the previous chapter. They are the PR and PI D-specs.
OK, enough titillation. Let’s create a simple program that prototype calls another very simple program. Now there’s a top 10 skill for me. Simple stuff.
Calling Program: DWS0996
Obviously, we will need two programs to do this, so here’s the first one, the calling program, DWS0996.
H********************************************************
H* DWS0996 – Calling Program
H********************************************************
H DFTACTGRP(*NO) ACTGRP(*NEW)
H********************************************************
F* Display File
FDWS0170FM CF E WORKSTN PREFIX(D01_)
F********************************************************
D*
D DWS0997 PR EXTPGM('DWS0997')
D D01_PRDNO 15
D MSG 60
D ERROR_FLAG 1
D
D ERROR_FLAG S 1
D MSG S 60
D
D*
/FREE
ERROR_FLAG = 'N';
CALLP DWS0997(D01_PRDNO:MSG:ERROR_FLAG);
*INLR = *ON;
/END-FREE
See, pretty simple really. But let’s break it down anyway.
H********************************************************
H* DWS0996 – Calling Program
H********************************************************
H DFTACTGRP(*NO) ACTGRP(*NEW)
H********************************************************
F* Display File
FDWS0170FM CF E WORKSTN PREFIX(D01_)
F********************************************************
We will start with the H- and F-specs. The H spec is needed to make sure this program gets compiled as ILE and that it is assigned to the activation group *NEW. If your compile command defaults to DFTACTGRP(*NO) instead of *YES, then you don’t have to bother with that parm, although as we will see later, you should set the activation group (otherwise it will default to QILE). You can use CALLP with OPM programs, so it won’t blow up if you don’t have that in there, but since this book is about ILE—sort of seems wrong not to go that direction. It’s the sub-procedures that have to be done in ILE. CALLP can be used in both ILE and OPM.
I then have only the display file, the theory being that I get my product number from that file. I know I don’t have logic in the program to read that file, but that just seemed like a distraction from what we are really doing.
D*
D DWS0997 PR EXTPGM('DWS0997')
D D01_PRDNO 15
D MSG 60
D ERROR_FLAG 1
D
D ERROR_FLAG S 1
D MSG S 60
D
D********************************************************
That brings up the D-specs. We have the prototype D-spec (the PR), but this time there is an additional parameter there, a keyword EXTPGM('DWS0997'), representing the program we are going to call.
Some people like to call the D-spec something other than the program name, like VAL_PRDNO or something like that. Something more English language-oriented that can easily be identified. And that is fine. It is not a sign of psychosis or anything bad. I just like using the program ID. Not very creative, I guess.
What is important is that the actual name of the program you want to call is in the EXTPGM keyword. If you fail to put in the EXTPGM keyword, the compile will fail in the binding stage. If you put in the EXTPGM keyword but use a value in it that is not a valid object, then the compile and bind will work, but the job will fail when you run it.
Then we define the ERROR_FLAG and the MSG since things aren’t really defined in the PR. We saw this in chapter 7, but it is worth remembering. And, as it always has, D01_PRDNO is in the DWS0170FM and so does not need to be defined separately.
Also, please note that the calling program has a PR spec but no PI spec.
/FREE
ERROR_FLAG = 'N';
CALLP DWS0997(D01_PRDNO:MSG:ERROR_FLAG);
*INLR = *ON;
/END-FREE
Then, finally, there are the actual code statements. Note that the CALLP statement is the same as the one that we used for the call to the sub-procedure in the previous chapter except that I am using the program ID now rather than VAL_PRDNO. You can do it either way, but the name of the D-spec is the one that must appear in the CALLP. So, you can’t really do it either way. What I meant is you have to use the name of the PR D-spec, but that name could be either DWS0997 or VAL_PRDNO. Please note there are no quote marks in this CALLP statement.
The Called Program: DWS0997
And now, the program you are calling, the program that represents the sub-procedure logic. Hang onto your hats, folks.
H********************************************************
H* DWS0997 – Called Program
H********************************************************
H DFTACTGRP(*NO) ACTGRP(*CALLER)
H********************************************************
F* PRODUCT MASTER
FMSPMP100 IF E K DISK PREFIX(PM_)
F********************************************************
D*
D DWS0997 PR
D D01_PRDNO 15
D MSG 60
D ERROR_FLAG 1
D DWS0997 PI
D D01_PRDNO 15
D MSG 60
D ERROR_FLAG 1
D********************************************************
/FREE
ERROR_FLAG = 'N';
//READ PRODUCT MASTER USING PRODUCT NUMBER FROM
//SCREEN AS KEY.
CHAIN(E) (D01_PRDNO) MSPMP100;
IF NOT %FOUND;
ERROR_FLAG = 'Y';
MSG = 'THIS IS A BAD PRODUCT NUMBER +
(XXX00111.01).';
ENDIF;
*INLR = *ON;
/END-FREE
Now, let’s take this baby apart.
H********************************************************
H* DWS0997 – Called Program
H********************************************************
H DFTACTGRP(*NO) ACTGRP(*CALLER)
H********************************************************
F* PRODUCT MASTER
FMSPMP100 IF E K DISK PREFIX(PM_)
F********************************************************
No surprises here. I included an H-spec to make sure (in my outdated environment) that I end up with an ILE program. And F-specs to define the Product Master that will be used for the validation. I don’t need the display file because that is in my calling program, and the value I entered there will be passed in via the prototype specs.
D*
D DWS0997 PR
D D01_PRDNO 15
D MSG 60
D ERROR_FLAG 1
D DWS0997 PI
D D01_PRDNO 15
D MSG 60
D ERROR_FLAG 1
D********************************************************
Now the D-specs look a little different. For one thing, there are two of them. We start with the PR, the prototype spec, just like we had in the calling program.
To that we now add the PI, the procedure interface, which we had used in the sub-procedure D-specs in chapter 7. The name for both of these D-specs must match—each other, you know. As must the subfields that are being specified. Otherwise you will get a compile error.
I haven’t included the SP_PRDNO—there’s no need. Since we are not using sub-procedures here, and so there is no need to show off local variables. I will just use D01_PRDNO from the display file.
There is one more thing here. Where are the definitions for the D01_PRDNO, MSG, and ERROR_FLAG field? And the answer is, even though the PR doesn’t really define a variable, the PI does. I don’t know the reason for that. Never asked, don’t really care. It makes no sense that one does and one doesn’t, but if I insisted that everything make sense, I would still be waiting.
/FREE
ERROR_FLAG = 'N';
//READ PRODUCT MASTER USING PRODUCT NUMBER FROM
//SCREEN AS KEY.
CHAIN(E) (D01_PRDNO) MSPMP100;
IF NOT %FOUND;
ERROR_FLAG = 'Y';
MSG = 'THIS IS A BAD PRODUCT NUMBER +
(XXX00111.01).';
ENDIF;
*INLR = *ON;
/END-FREE
And finally, the logic statements. They are identical to what we used in the sub-procedure except that I am just using the D01_PRDNO field and not bothering with the SP_PRDNO local variable.
LATEST COMMENTS
MC Press Online