3.7 Parameters and ArgumentsThe terms parameter and argument are often used interchangeably, although they have entirely different meanings. Let us illustrate with an example. Consider the following function, which replicates a string a given number of times: Function RepeatString(ByVal sInput As String, ByVal iCount As Integer) _ As String Dim i As Integer For i = 1 To iCount RepeatString = RepeatString & sInput Next End Function The variables sInput and iCount are the parameters of this function. Note that each parameter has an associated data type. Now, when we call this function, we must replace the parameters by variables, constants, or literals, as in: s = RepeatString("Donna", 4) The items that we use in place of the parameters are called arguments. 3.7.1 Passing ArgumentsArguments can be passed to a function in one of two ways: by value or by reference. Incidentally, argument passing is often called parameter passing, although it is the arguments and not the parameters that are being passed. The declaration of RepeatString given earlier contains the keyword ByVal in front of each parameter. This specifies that arguments are passed by value to this function. Passing by value means that the actual value of the argument is passed to the function. This is relevant when an argument is a variable. For instance, consider the following code: Sub Inc(ByVal x As Integer) x = x + 1 End Sub Dim iAge As Integer = 20 Inc( iAge ) Msgbox( iAge ) The final line: Msgbox( iAge ) actually displays the number 20. In other words, the line: Inc( iAge ) does nothing. The reason is that the argument iAge is passed to the procedure Inc by value. Since only the value (in this case 20) is passed, that value is assigned to a local variable named x within the procedure. This local variable is increased to 21, but once the procedure ends, the local variable is destroyed . The variable iAge is not passed to the procedure, so its value is not changed. On the other hand, if we modify the definition of the procedure Inc , replacing ByVal with ByRef , the story is different: Sub Inc(ByRef x As Integer) x = x + 1 End Sub In this case, what is passed to the procedure Inc is a reference to the argument iAge . Hence, the procedure actually operates on the variable passed to it, incrementing the value of iAge to 21. Put another way, the variable represented by the parameter x is actually the passed variable iAge . In VB.NET, the default method of argument passing for arguments is by value. This is a change from earlier versions of VB, in which the default method was by reference. 3.7.2 Passing ObjectsThere is a subtlety in argument passing with parameters of any object type. Actually, the subtlety occurs because an object variable is a pointer ; that is, it contains a reference to (or the address of) the object. If we pass an object variable by value, we are passing the contents of the variable, which is the address of the object. Thus, any changes made in the called procedure affects the object itself, not a copy of the object. This seems like passing by reference, but it is not. Think of it this way: passing the value of an object's address is passing a reference to the object. On the other hand, if we pass an object variable by reference, we are passing the address of the variable. In other words, we are passing the address of the address of the object! In languages that support pointers, this is referred to as a double pointer . Let us illustrate with an example. Consider the following code, and imagine that the form containing this code has two textboxes: TextBox1 with text "TextBox1" and TextBox2 with text "TextBox2": Public Function GetText(ByVal txt As TextBox) As String ' Change reference to textbox txt = Textbox2 End Function Sub Doit Dim t As TextBox t = TextBox1 GetText( t ) msgbox(t.Text) ' Displays TextBox1 when ByVal, _ ' TextBox2 when ByRef End Sub Now, here is what happens when we execute DoIt . Note that the argument is passed to GetText by value in this case.
Now suppose we change the ByVal keyword to ByRef in GetText . Here is what happens:
3.7.3 Optional ArgumentsIn VB.NET, parameters can be declared as optional using the Optional keyword, as shown in the following code: Sub Calculate(Optional ByVal Switch As Boolean = False) In VB.NET, all optional parameters must declare a default value, which is passed to the procedure if the calling program does not supply that parameter. The following rules apply to optional arguments:
Note that in earlier versions of VB, you could omit the default value and, if the parameter was of type Variant, you could use the IsMissing function to determine if a value was supplied. This is not possible in VB.NET, and the IsMissing function is not supported. 3.7.4 ParamArrayNormally, a procedure definition specifies a fixed number of parameters. However, the ParamArray keyword, which is short for Parameter Array, permits us to declare a procedure with an unspecified number of parameters. Therefore, each call to the procedure can use a different number of parameters. Suppose, for instance, that we want to define a function to take the average of a number of test scores, but the number of scores may vary. Then we declare the function as follows : Function GetAverage(ByVal ParamArray Scores( ) As Single) As Single Dim i As Integer For i = 0 To UBound(Scores) GetAverage = GetAverage + CSng(Scores(i)) Next GetAverage = GetAverage / (UBound(Scores) + 1) End Function Now we can make calls to this function with a varying number of arguments: Msgbox(GetAverage(1, 2, 3, 4, 5)) Msgbox(GetAverage(1, 2, 3)) The following rules apply to the use of ParamArray :
|