Optional Parameters

Colin Campbell points out that it is not necessary to pass a parameter which the subprogram will not use in a given context. He cites a date routine with multiple action codes, of which some require a second parameter. For other action codes, the subprogram does not access the second parameter, and the calling program needn't pass it.

I'm willing to believe that this trick can work (I haven't tried it). Nevertheless I don't regard it as good practice. It is just another way for the calling program to call the same routine in different ways under different circumstances. In order to do so successfully, the calling program must display an unhealthy familiarity with the innards of the subprogram. The potential for confusion is just as great as if you passed a dummy parameter.

In any case this trick will work only if all the omitted parameters are at the end of the parameter list. You can't omit just the third parameter out of four.

On the other hand...

Despite all that I have said above, I must grudgingly admit that the missing-parameter trick offers one advantage.

Suppose you pass only three of the four parameters, expecting that the subprogram will not access the fourth one in a given case. If you are wrong, and the subprogram does try to access the fourth parameter, your mistake will become visible in the form of an abend.

Consider the analogous situation with multiple entry points, each one taking a different combination of parameters. Through a coding error, one of the entry points accesses a parameter which doesn't belong to it. Since the LINKAGE SECTION defines that parameter, the compiler doesn't complain. At runtime, however, the entry point may access a parameter passed on a previous call to some other entry point.

If you like to be devious, you might let this situation occur deliberately (I'm not recommending it!). More likely you will spend a lot of time tracking down mysterious bugs -- or even more time not realizing that the bugs are present.


[home]COBOL Home [style forum]COBOL Style Forum