Maybe you don’t want to just use a sub-procedure in your program. Maybe you actually 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.
Editor's Note: This article is excerpted from article 8 of 21st Century RPG: /Free, ILE, and MVC, by David Shirey.
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 article: PRACTICAL ILE - ILE Subroutines to Sub-Procedures. 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. I am using the same basic program that I used for the previous article. I figure that way you at least have a feel for what I am doing. Here it is.
See, pretty simple really. But let’s break it down anyway.
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.
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 the previous article, 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.
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 article 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.
Now, let’s take this baby apart.
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.
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 the previous article. 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.
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.
What Ya Shoulda Learned
Basically, a lot of the same stuff that you should have learned in the last article. In fact, both program sets are almost identical, aren’t they?
So which one is better?
Using the sub-procedure or the separate program, I mean? And the answer, like most things is: it depends.
Certainly the sub-procedure is more efficient, although with the new CALLP they are both pretty quick. If you get hung up worrying about which is faster, then you really need to switch to decaf.
Obviously, there are advantages to putting the validation logic in a separate program so it can be more easily used in other situations. Although, as we will see in the next article, we can make a sub-procedure available to multiple programs without using a separate program call.
Also, please note that I assume you did this by compiling both programs with CRTBNDRPG. But you could have also used CRTRPGMOD on both of them and then CRTPGM to bind them together. We will talk more about the differences in these two approaches later. For now, let’s keep things simple, shall we?
Before we can decide which is best, there is one more option.
Yes, you guessed it. A dark and dangerous option. An option that is spoken of only in whispers where the ragged IT people go. An option so terrifying and powerful that few mere mortals have attempted to capture and tame it.