Private Sub Swap (By Ref x As Integer, By Ref y As Integer)

Dim temp As Integer



End Sub

Private Sub Display Vals(By Ref a As Integer, By val b As Integer)

‘Don’t worry about understanding the next line yet, it will be explained later.

Msg Box “a=” & C str(a) vb crl f & ”b=” & c str(b)

End Sub

Private Sub Form-Load ()

Dim a As Integer

Dim b AS Integer



Display values, swap, & display again

Display vals a,b

The next line is functionally identical to “swap a, b” Call swap (a, b)

Display vals a, b

End Sub

Notice that call was used instead of simply stating the Subroutine name. When using the Call method however, you must use parenthesis when calling the Subroutine. Note that this program would also have worked without typing “By Ref” anywhere, Since it is the default. The By Ref & By Val keywords are raredy used in simple programs, however, but it’s a nice trick for toolkit.


Subroutines have a close cousin called Functions. Functions are basically the exact same as Subroutines. Except that they return a value. That means that the functions itself has a type, and the function will return a value to the calling Subroutine based on the code that it contains.

An example of this would be a function that adds two numbers, shown below. A function is declared the exact same way as a Subroutine, except using the “Function” keyword instead of “Sub”. To return a value, assign a value of the proper type to the function’s name, as if it were a variable.

Private Function Add (By Val x As Integer By Val y As Integer) As Integer

Add =x+y

End Function

Private Sub Form-Load()

Dim a As Integer

Dim b As Integer

Dim c As Integer



c=Add (a, b)

Msg Box c

End Sub

The different between a Sub and a Function is that a Sub does not produce a return value (i.e., one that can be assigned directly to a variable, where as a Function does produce a return value). Both Subs and Functions can be called with or without parameters.


In the “Try It” example below, upon clicking the “Try It” button, the user is prom pled flow then returns to the Print statement, which displays the sum of the two numbers on the form.

Note that the variables involved are declared at the module-level (i.e., general declarations section, prior to any Sub or Function) because the variable are common to both the Cmd Try It. Click event procedure & the Add Em Sub procedure.


Option Explicit

Module-level variables are needed for this example.

Dim mint Num1 As Integer Dim mint Num2 As Integer

Dim mint Sum As Integer…

Private Sub Cmd Try It-Click ()

Mint Num1=val (Input Box (“Enter first number:”, “Add Program”)).

Mint Num2=val (Input Box (“Enter second number:”, “Add Program”)).

Add Em

Could also be coded as “Call Add em()” with or without the  ()

Private “The Sum is”; mint sum

End Sub

Private Sub Add Em()

Mint Sum= mint Num1 + mint Num2

End Sub


Private Function Display (x, y)

Display = x And 10

End Function

Private Sub Form-Load ()

Dim a as String

Dim b as Integer

a= “Sara”


c=Display (a, b)

Display c

End Sub.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s