PROCEDURE ARGUMENTS

The argument list provides a means to control the flow of information between the calling program and the procedure. The procedure's arguments are collectively referred to as its parameters list, and each argument is called a parameter.

There is a two-way communication between the procedure and the main program.

The subroutine needs data to act on and must return it result, to be utilized in the main program. The data and results are said to be "passed" between the procedures.

Notice that the arguments in the calling program may or may not have variable names different from those in the procedure.

The following example (ParamList.xls) illustrates the behavior of the parameters in a Sub procedure.

Sub Test()
    'assign values
    a = 1
    b = 2
    c = 3
    MsgBox a & " " & b & " " & c & " " & d
    Call Arg(a, b, c)
    MsgBox a & " " & b & " " & c & " " & d
End Sub

Sub Arg(b, a, d)
    MsgBox a & " " & b & " " & c & " " & d
    d = 4
    c = b
    MsgBox a & " " & b & " " & c & " " & d
End Sub

When you run Sub Test, the first Msgbox yields 1 2 3. Thus a, b, and c have their original assigned values of 1, 2, and 3, respectively. The variable d is displayed as a blank because it has not been assigned a value.

After the OK button is clicked, the Sub procedure Arg is called. The values of a, b, and c are passed to Arg, where they are temporarily associated with the variable names b, a, and d, respectively.

Thus when the second Msgbox statement is executed, the result is displayed as 2 1   3.

Therefore the values that are passed as parameters b, a, and d initially have values related to the variables a, b, and c in the main program. Hence, a = 2, b = 1, and d = 3. Because c does not appear in the parameter list, it has no value within Arg.

After OK is clicked, d is assigned a value of 4 and c is assigned a value of 1 by the statement c = b. The resulting Msgbox is displayed as 2 1 1 4.

After OK is clicked now, the results for b, a, and d are passed back to the calling Sub, where they are assigned to a, b, and c, respectively. Hence the final Msgbox is displayed as 1 2 4.

Notice how the value of 4 is assigned to the variable c because of its position in the parameter list. On the other hand, the assignment in Arg, d = 4, has no effect on the variable in the main program. This is because d is not included in the Call parameter list. Therefore the two d's are distinct.

Variable that are used exclusively within a procedure are referred to as local variables. In the above example, the variable c in the sub Arg was local. Because c is not "passed" back to the main program, the value of c = 1 holds only in Arg and has no effect on the value of c in the main program.

The ability to hold local variables in a procedure serves to make each subroutine a truly autonomous entity;
  1. Changing a local variable will have no effect on variables outside the subroutine.
  2. Local variables can even have the same name as variables in other parts of the program, yet still ne distinct entities.
  3. Advantage: it decreases the likelihood of "side effects" that occurs when the value of a global variable is changed inadvertently in a subprogram.
  4. Existence of local variables facilitates the debugging of a program. Subprograms can be debugged individually without fear that a working subroutine will cease functioning because of an error in another subroutine.