Function Definitions

advertisement
Function Definitions
1.
2.
Dim n As Integer
Dim MyNumber As Double
3. Function Fib(Byref sample() as Integer, byval Top As Integer ,byval Bottom as integer) As Integer
4. For ABC = Bottom To (Top-1)
5.
counted loop from 1 past ABC to Top
6.
For cnt = (ABC + 1) To Top
7.
'compare which number is greater to put highest on top
8.
If sample(ABC) < sample(cnt) Then
Function Statement
9.
'swap the numbers
Declares the name, arguments, and code that form the body of a Function
10.
temp = sample(ABC)
procedure.
11.
sample(ABC) = sample(cnt)
Syntax:
12.
sample(cnt) = temp
13.
End If
Function name [(arglist)] [As type]
14.
Next cnt
[statements]
15. Next ABC
[name = expression]
16. End Function
17. Function SumMyArray(x() As Integer) As Integer
18.
19.
20.
21.
22.
23.
24.
thesum = 0
ArraySize = UBound(x())
For w = 0 To ArraySize
thesum = thesum + x(w)
Print thesum, w, x(w)
Next w
SumMyArray = thesum
[Exit Function]
[statements]
[name = expression]
End Function
by value A way of passing the value of an argument to a
procedure instead of passing the address. This allows
the procedure to access a copy of the variable. As a
result, the variable's actual value can't be changed by the
procedure to which it is passed.
25. End Function
26.
27.
28.
29.
30.
31.
32.
33.
34.
by reference A way of passing the address of an
' The following user-defined function returns the square root of the argument to a procedure instead of passing the value.
' argument passed to it.
This allows the procedure to access the actual variable.
Function CalculateSquareRoot(NumberArg As Double) As Double
As a result, the variable's actual value can be changed by
If NumberArg < 0 Then ' Evaluate argument.
the procedure to which it is passed. Unless otherwise
Exit Function ' Exit to calling procedure.
specified, arguments are passed by reference.
Else
CalculateSquareRoot = Sqr(NumberArg) ' Return square root.
End If
End Function
35. Private Sub Command1_Click()
36.
37.
38.
39.
40.
n = InputBox("Enter an INTEGER")
For x = 0 To (n - 1)
Print Fib(CInt(x))
Next x
End Sub
41.
42.
43.
44.
Private Sub Command2_Click()
MyNumber = Val(InputBox("Enter a Number", "Value", 9))
Print CalculateSquareRoot(MyNumber)
End Sub
45. Private Sub Command3_Click()
46. Dim MyNum(5) As Integer
47. ArraySize = 5
48. For z = 0 To ArraySize
49. MyNum(z) = Val(InputBox("Enter a Number", "Value", 9))
50. Next z
51. Print SumMyArray(MyNum())
52. End Sub
Download