Home » Programming » 07 - Subprograms
7

Parameters and Parameter Passing Methods

Parameters in a subprogram must match the parameters in the subprogram definition.

A subprogram is basically a program within a program. It performs a specific function and is independent of the program itself. It uses the data structures provided by the programming language. A subprogram is activated when it is invoked at runtime and it is ended once it has completed execution.

Parameters and Parameter Passing Methods

Parameters in a subprogram must match the parameters in the subprogram definition. There must be actual parameters to fill in the predefined formal parameters. For example, there is a subprogram that connects an x, y, z coordinate to the point 0, 0, 0 with a line and displays it on the screen. In the subprogram 3dline(x, y, z), the formal parameters are x, y, z, while sample values may be 10, 11, 12. If the last parameter is lacking then the subprogram will fail to execute.

Correspondence is also important when using subprograms. For example, if a subprogram requires an integer as the first parameter and text on the second parameter then inputs must correspond to them. For example, in a subprogram guests(a, class), a must be an integer and class must be VIP, family, friend, or acquaintance. If a user enters guests(2, 1) then the subprogram will fail to execute. Examples of valid inputs are guests(4, VIP), guests(7, friend), and guests(3, family).

Parameter passing implementation methods fall under several types. The first type of parameter passing is the call by value. In this method, the formal parameters are allocated memory on the stack. The actual parameters simply take on the memory of the formal parameters. The second type of parameter passing is the call by result. In this method, the formal parameter is allocated storage on the stack. Instead of the actual parameters taking on the memory of the formal parameters, it is the other way around. The formal parameters take the memory that the existing actual parameters had. The third type of parameter passing is the call by value result. This simply combines the first two. On entry to the subprogram, formal parameters are allocated on the stack. The actual parameters take on the memory of the formal parameters. On exit from the subprogram, the formal parameters take on the memory that the actual parameters occupied.

The fourth type of parameter passing method is the call by reference. Upon entry to the subprogram, the formal parameters take on the allocated storage. The formal parameter renames the actual parameters. During the execution of the program, any modification to the formal parameter will affect the actual parameter. The value of the actual parameter is set by its last assignment. The fifth type of parameter passing method is the call by name. In this method, each formal parameter stands for the actual evaluation of the particular actual parameter. The references to the formal parameters require an evaluation of the corresponding actual parameters. Evaluation is only made until the actual parameters are actually referenced within the subprogram.

Use your Visual Basic editor and try making a subprogram. In the Tutorial Program that you have been practicing on, make another program outside of it but within the same module. In the example below, a function that multiplies two numbers was made. Notice that in the Tutorial Program, the Multiplier Function was called. The Multiplier Function is now a subprogram of the larger Tutorial Program.

If you want to try parameter passing then why not try to enter words instead of numbers in the multiplier function. See what happens. Does the program run or does it fail to execute?