GBIC: Functions and Subroutines
The following is reprinted for archival purposes from Gary Beene's Information Center, with permission from Mr. Beene himself.
'VB supports two kinds of functions - Subroutines and Functions
'Both allow passing variables to the Sub/Function for use
'Subroutines execute code and return control of program to calling point
Sub MySub ()
Print "Hello"
End Sub
'Functions execute code, return a value to the program and return control of program to calling point
Function MyFunction () As String
MyFunction = Date
End Function
'usage
MyString = MyFunction 'Date is placed in MyString
'Passing variables - Sub declaration defines data type of passed variable
Sub MySub (i As Long , s As String , a() As Variant )
Print i
Print s
Print a(5)
End Sub
'Passing variables - Sub declaration defines how passed variables are treated
'ByVal cannot alter the passed variable
'ByRef can alter the passed variable
Sub MySub ( ByVal i As Long , ByRef s As String )
i = 5 'the original variable passed has not changed - ByVal
s = 10 'the original variable passed is now changed - ByRef
End Sub
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.