Parameters and Arguments
The 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.
Passing Arguments
Arguments 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 ...
Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.