O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required