H o m e

 

S u b    S t a t e m e n t    v s .    F u n c t i o n    S t a t e m e n t    

By Michael McIntyre

mikemc@getdotnetcode.com

Mike McIntyre

 

This article is for people learning to program with the Visual Basic language who are not sure of the difference between a Sub statement and a Function statement, and when and how to use either one.

 

Example – Sub Statement

 

The Sub statement is used to execute one or more lines of code, period.

 

' SayHello Sub

Private Sub SayHello(ByVal nameOfPerson As String)

    ' Declare a variable named message of type String.

    Dim message As String

    ' Combine the string literal "Hi " with the nameOfPerson String value

    ' passed into this Sub assigning the resulting String to the message variable.

    message = "Hi " & nameOfPerson

    ' Use the MessageBox class' Show method to show the message String to the user.

    MessageBox.Show(message)

End Sub

 

Code that calls the SayHello Sub:

 

    ' Call the SayHello Sub passing it a String.

    SayHello("Mike")

 

Result:  A MessageBox opens displaying the message “Hi Mike”.

 

Example – Function Statement

 

The Function statement is used to execute one or more lines of code AND return a value to the caller.

 

' CalculateSalesTax Function

Private Function CalculateSalesTax(ByVal salesTaxRate As Decimal, ByVal saleAmount As Decimal) As Decimal

    ' Declare a variable of type Decimal named salesTax.

    Dim salesTax As Decimal

    ' Muliply the salesTaxRate Decimal times the saleAmount Decimal

    ' (both passed into this function) assigning the

    ' resulting Decimal to the salesTax variable.

    salesTax = salesTaxRate * saleAmount

    ' Return the salesTax Decimal value to the code calling this Function.

    Return salesTax

End Function

 

Code that calls the CalculateSalesTax Function:

 

    ' Declare a variable of type Decimal named calculatedSatesTax.

    ' This variable will hold the value returned

    ' from calling the CalculateSalesTax Function.

    Dim calculatedSalesTax As Decimal

    ' Call the CalculateSalesTax Function passing in

    ' two Decimal values.

    calculatedSalesTax = Me.CalculateSalesTax(0.05, 100)

    ' Use the MessageBox class' Show method to show the

    ' calculatedSalesTax to the user.

    MessageBox.Show("Sales tax is: " & calculatedSalesTax.ToString)

 

Result:  A MessageBox opens displaying the message “Sales Tax is: 5”.

 

Things to Notice

 

Use a Sub when you do not need to return a value to the calling code.  Use a Function when you need to return a value.

 

Notice the ‘As Decimal’ at the end of the first line of the Function.  This is important.  If you do not have OPTION STRICT ON, it is possible to leave this off.  Leaving off the ‘As [value]’ part of a function is not good object-oriented programming practice. This is one of may reasons to turn on OPTION STRICT.

 

Notice the use of ‘ByVal’ in the parameters of both the Sub and the Function.  While the code would still work without it because ‘ByVal’ is implicit (assumed by the VB compiler if you leave off ‘ByVal’; it is good object-oriented coding practice to explicitly add ‘ByVal’ (add ByVal to your code).

 

Summary

 

While you could, with OPTION STRICT OFF, create the following function – you should now be able to recognize the function is poorly written:

 

' BAD SayHello Function

Private Function SayHello(nameOfPerson As String)

    ' Declare a variable named message of type String.

    Dim message As String

    ' Combine the string literal "Hi " with the nameOfPerson String value

    ' passed into this Sub assigning the resulting String to the message variable.

    message = "Hi " & nameOfPerson

    ' Use the MessageBox class' Show method to show the message String to the user.

    MessageBox.Show(message)

End Function

 

What’s wrong?  It returns no value so a Sub, not a Function, should have been used.  It is a function but the ‘As [type]’ part of the first line is missing.  ‘ByVal is missing from the first line.

 

To learn more about Visual Basic.NET Sub and Function statements click these links:

 

Sub

Function

 

Copyright © 2001-2005 aZ Software Developers. All rights reserved.