About Reference Parameter Subprogram Example B
In come cases, such as the Swap subprogram in Reference Parameter Example A, the use of reference parameters is essential. In other cases, use of reference parameters may not be essential, but may be prudent. The classic case is a situation in which very large arrays - hundred or even thousands of elements - are involved. In such situations, making copies of such arrays is to be avoided whenever possible. This demo illustrates some techniques that are applicable in such situations.
An existing array is to be filled with specific data. Making a copy of the array in question is not appropriate. The subprogram needs to operate directly on the array.
Things to note:
-
The formal parameter X (the one called by reference) becomes an alias for the actual array Arr in the client program. Operations on X in the subprogram are actually performed on the array Arr in the main program.
- The values of the StartValue, Increment, and N variables in the main program are copied to Start, Inc, and Lim in the subprogram, because these parameters are being passed by value.
- The subprogram can, and does, modify the value of Start without affecting the value of StartValue in the main program.
Some things to discuss in this week's conference:
- What would a programmer have to do to accomplish the task performed by the subprogam, if call by reference were not supported by the programmming language system that the programmer is using?
- The main program in this example was written in order to test the subprogram, so no attempt at input data validation was made. If this were intended to be a part of a production program, what validation criteria should be applied to the inputs?