Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

12 Functions / subroutines


'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