Wyo VB Lecture Notes
Objective #1 - Create and use functions.
- You have already learned how to create your own method and how to call that method from a call statement. There's another variation
of a method that can be used to compute a value and return that value to the call statement. This type of method is often called a function.
- A task that involves the computation of a single numerical value such as computing tax or rounding a number is often handled by a function rather than a normal method. If the task
that you wish to place into a method does NOT involve the computation of single numerical value then you should probably use a normal method instead of a function. For example, if you need to move
an object to the left on the form window, you could use a normal method named move.
- A function is different from a normal method in that a function always returns a single value to the method or function that calls it. A normal method does not return a value.
- Only one value can be returned by a function.
- Here is an example of a function. The following function named GetRandom generates a random integer between 1 and 6 and returns that value:
Private Function GetRandom() As Integer
Dim randomNum As Integer = 0
randomNum = Math.Floor(Rnd() * 6) + 1
Return randomNum
End Function
- The first line of a function is called the function header. The header of the GetRandom function above is
Private Function GetRandom() As Integer
- The name that you give to a function should describe its purpose. The example above is named GetRandom because it can be used to get a random number.
Often a verb is used somewhere in the name of a method implying some action that is performed.
- The return type of a function is the data type that is typed after the keyword As in
the function header. This must match the data type of the value that is returned by the function's Return statement. If the variable that is being returned is an Integer such
as the variable randomNum above then the As Integer must be typed at the end of the function header. In this VB course, we will typically
be returning the data types Integer, Double, String or Boolean.
- A function can have parameters. If there are any parameters, they must be typed in the parentheses of the function header. The following variation of the
example above allows the user to determine the range in which the random integer is generated.
Private Function GetRandom(ByVal numSides As Integer) As Integer
Dim randomNum As Integer = 0
randomNum = Math.Floor(Rnd() * numSides) + 1
Return randomNum
End Function
- The word ByVal must
be typed in front of each parameter in a function header and the data type of the parameter must be typed after the keyword As. DO NOT also declare the parameter as
a variable with a Dim statement in the body of the function.
Private Function GetRandom(ByVal numSides As Integer) As Integer
Dim numSides As Integer = 0 'DO NOT DO THIS
Dim randomNum As Integer = 0
randomNum = Math.Floor(Rnd() * numSides) + 1
Return randomNum
End Function
- Using the keyword ByVal means
that the actual parameter is being passed by value and that it will not be changed even if the corresponding formal parameter in the called function is changed. Using the word ByRef instead
of ByVal causes any changes in the corresponding formal parameter to affect the actual parameter. Passing a parameter by reference (i.e. with ByRef)
is more dangerous but can save memory when passing large objects rather than numbers. There are times though when you need to pass a large object and passing by reference is worthwhile. In this VB
course, you will not be responsible for knowing how to pass ByRef nor will you probably need to pass by reference.
- Here is yet another example of a function that has two parameters
Private Function GetRandom(ByVal low As Integer, ByVal high As Integer) As Integer
Dim randomNum As Integer = 0
randomNum = Math.Floor(Rnd() * (high - low + 1)) + low
Return randomNum
End Function
- The version below is rewritten without declaring any local variables (i.e. Dim statements):
Private Function GetRandom(ByVal low As Integer, ByVal high As Integer) As Integer
Return Math.Floor(Rnd() * (high - low + 1)) + low
End Function
- Another example of a function is
Private Function ComputeTax(ByVal price As Integer) As Double
Return price + price * 0.06
End Function
Notice that the return value is a Double even though the parameter is an Integer.
The programmer must realize that when you multiply an Integer by 0.06, the product is usually a decimal number (i.e. Double).
- Another example of a function that returns a boolean True value if a bullet hits an enemy where both are picture boxes on the form. Notice that
the parameters are PictureBox objects!
Private Function HitEnemy(ByVal picEnemy As PictureBox, ByVal picBullet As PictureBox) As Boolean
If (picBullet.Top < picEnemy.Bottom And picBullet.Bottom > picEnemy.Top And picBullet.Left < picEnemy.Right And picBullet.Right > picEnemy.Left) Then
Return True
End If
Return False
End Function
- Another example of a function that returns the median of three parameters
Private Function FindMedian(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer
If (x > y And x < z Or x < y And x > z) Then
Return x
ElseIf (y > x And y < z Or y < x and y > z) Then
Return y
End If
Return z
End Function
Objective #2 - Call functions from other methods or functions.
- The main reason to write functions is to be able to reuse them. To use a function you must call it from another method or function. This is done with a call
statement.
- Here is an example
of a method named btnRollDice_Click which includes a call statement that calls the method named GetRandom. GetRandom generates
a random integer between 1 and 6 and returns that value to the call statement in the btnRollDice_Click method where the value is immediately assigned and stored into
the variable num
Private Sub btnRollDice_Click(. . .)
Dim num As Integer = 0
num = GetRandom()
lblOutput.Text = num
End Sub
Private Function GetRandom() As Integer
Dim randomNum As Integer = 0
randomNum = Math.Floor(Rnd() * 6) + 1
Return randomNum
End Function
The call statement is
num = GetRandom()
and the value that is returned by GetRandom is immediately stored and assigned to the local variable num.
- The following variation of the example demonstrates how a parameter can be passed to the function. The act of passing a parameter from one method or function to
another method or function is called passing a parameter. This allows the user to determine the range in which the random integer is generated. Any
number that is typed into the textbox named txtInput is passed to the GetRandom function's parameter named numSides.
Private Sub btnRollDice_Click(. . .)
Dim num As Integer = 0
Dim numSides As Integer = 0
numSides = Val(txtInput.Text)
num = GetRandom(numSides)
lblOutput.Text = num
End Sub
Private Function GetRandom(ByVal numSides As Integer) As Integer
Dim randomNum As Integer = 0
randomNum = Math.Floor(Rnd() * numSides) + 1
Return randomNum
End Function
Notice that the name of the parameter in the call statement, numSides, does not have to match the name of the parameter in the function header. Visual Basic knows
to pass the numeric value stored in numSides to the parameter numSides even though they have different names.
- Passing parameters wisely allows you to not only reuse methods and functions but it also reduces the need for module and global variables.
- If there are two or more parameters in the parentheses of the call statement, they match up in a one-to-one correspondence with the parameters that are listed in the function header.
The data types for the parameters in the call statement must exactly
match the order of the data types in the function header. In other words, you should not pass a parameter that is a Double to a parameter that is an Integer.
Here is yet another example of calling a function where two parameters are passed
Private Sub btnRollDice_Click(. . .)
Dim num As Integer = 0
num = GetRandom(10, 20)
lblOutput.Text = num
End Sub
Private Function GetRandom(ByVal low As Integer, ByVal high As Integer) As Integer
Dim randomNum As Integer = 0
randomNum = Math.Floor(Rnd() * (high - low + 1)) + low
Return randomNum
End Function
The value 10 is passed to the parameter low and the value 20 is passed to the parameter high. A logic error would occur if the
programmer mistakenly typed 20, 10 instead of 10, 20 since VB would plug in the wrong numbers to the wrong parameters.
- The version below is rewritten without declaring any local variables (i.e. Dim statements):
Private Sub btnRollDice_Click(. . .)
lblOutput.Text = GetRandom(10, 20)
End Sub
Private Function GetRandom(ByVal low As Integer, ByVal high As Integer) As Integer
Return Math.Floor(Rnd() * (high - low + 1)) + low
End Function