Tracing and Writing Functions WS 2

advertisement
Visual Basic
Tracing and Writing Functions WS 2
Public Class Form1
Private Sub Form1_Load(. . .)
Label1.Text = Factorial(4)
Name_________________________
Period________
1_____________
Label2.Text = Weird(AddFive(3))
2_____________
Label3.Text = Factorial(2) + AddFive(-5)
3_____________
Label4.Text = Max2(-3, 4, 5)
4_____________
Label5.Text = Max(5, 8, -4)
5_____________
Label6.Text = Max(1, 2, Max(9, 3, 2))
6_____________
Label7.Text = Factorial(2 + 1)
7_____________
Label8.Text = Max(Factorial(2), Factorial(4), 0)
8_____________
Label9.Text = Weird(0 - 4)
9_____________
Label0.Text = Weird(Weird(3))
10____________
End Sub
End Class
Private Function Factorial(ByVal intNum As Integer) As Integer
Dim J As Integer = 1
Dim intProduct As Integer = 1
For J = 1 To intNum
intProduct = intProduct * J
Next
Return intProduct
End Function
Private Function AddFive(ByVal intNum As Integer) As Integer
Return intNum + 5
End Function
Private Function Weird(ByVal intNum As Integer) As Integer
If (intNum > 0) Then
Return 10
End If
Return 3
End Function
Private Function Max(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer
If (x > y And x > z) Then
Return x
ElseIf (y > x And y > z) Then
Return y
End If
Return z
End Function
Private Function Max2(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer
Return Math.Max(x, Math.Max(y, z))
End Function
Writing Functions WS
1. Write a function named Summation that is passed an Integer parameter named intNum. The function must compute and return
the sum of the numbers 1 through intNum. For example, if intNum is 5, then 15 is returned since 1 + 2 + 3 + 4 + 5 is 15.
2. Write a function named Min that accepts three Integer parameters. The function must return the smallest of the three
parameters. You may choose the names of the parameters.
3. Write a function named Median that the median value of three Integer parameters. For example, if the three parameters are 12, 9, and 3 then the value 3 is returned because 3 is between -9 and 12. You may choose the names of the parameters. You may
assume that all three parameters are unique values and different. It is recommended that you use an If ElseIf statement.
Download