260 Part II Programming Fundamentals
Percent = Hits / Tries
Return Format(Percent, "0.0%")
End Function
After you type the first line of the function code, Visual Basic automatically adds
an End Function statement. After you type the remainder of the function’s code, your
screen looks like this:
The HitRate function determines the percentage of wins by dividing the Hits argument
by the Tries argument and then adjusts the appearance of the result by using the
Format function. The HitRate function is declared as a string because the Format
function returns a string value. The Hits and the Tries arguments are placeholders for
the two short integer variables that will be passed to the function during the^ function
call. The HitRate function is general-purpose enough to be used with any shorter
integer numbers or variables, not only with Wins and Spins.
- Display the form again, and then double-click the Spin button on the Form1 .vb form to
bring up the Button1_Click event procedure. - Below the fourth line of the event procedure (Label3.Text = CStr(Int(Rnd() * 10))), type
the following statement:
Spins = Spins + 1
This statement increments the Spins variable each time the user clicks Spin, and new
numbers are placed in the spin windows.
- Scroll down in the Code Editor, and then, between the End If and the End Sub statements,
type the following statement as the last line in the Button1_Click event procedure:
lblRate.Text = HitRate(Wins, Spins)
As you type the HitRate function, notice how Visual Studio automatically displays the
names and types of the arguments for the HitRate function you just built (a nice touch).
The purpose of this statement is to call the HitRate function by using the Wins and
the Spins variables as arguments. The result returned is a percentage in string format,