- Sub Procedures
- Creating Functions
- Using Optional Arguments
- Passing a Variable Number of Arguments
- Preserving Data Between Procedure Calls
- Understanding Scope
- Handling Runtime Errors
- Unstructured Exception Handling
- Structured Exception Handling
- Introducing Classes and Objects
- Summary
- Q&A
- Workshop
Creating Functions
You can also create functions in Visual Basic .NET. They are just like Sub procedures except that they can return a value. You declare a function in much the same way as a Sub procedure, except that you use the Function keyword instead of Sub.
Let's look at an example. In this case, we'll create a function named Summer that calculates the sum of two integers and returns that sum; this project is named Functions in the code for the book. To create this new function, you use the Function keyword:
Module Module1 Sub Main() End Sub Function Summer(ByVal int1 As Integer, ByVal int2 As Integer) As Long End Function End Module
This example looks just like creating a Sub procedure, except for the Function keyword and the As Long at the end. The As Long part is there to indicate that this function returns a Long value. (Long is a better return value choice than Integer here because two integers could be passed to Summer such that their sum exceeds the capacity of the Integer data type.)
TIP
It's worth realizing that function names can use the same type prefixes that variable names use to indicate return type, such as intSummer.
You return a value from a function with the Return statement, as here, where the code is returning the sum of the two arguments passed to the function:
Module Module1 Sub Main() End Sub Function Summer(ByVal int1 As Integer, ByVal int2 As Integer) As Long Return int1 + int2 End Function End Module
Now when you call Summer with two integers, like Summer(2, 3), Visual Basic will treat that function call as an expression and replace it with the value returned by the function, which is 5 here. Listing 3.2 shows how this might look in code.
Listing 3.2 Returning Data from a Function (Functions project, Module1.vb)
Module Module1 Sub Main() Dim intItem1 As Integer = 2 Dim intItem2 As Integer = 3 Console.WriteLine(intItem1 & " + " & _ intItem2 & " = " & Summer(intItem1, intItem2)) Console.WriteLine("Press Enter to continue...") Console.ReadLine() End Sub Function Summer(ByVal int1 As Integer, ByVal int2 As Integer) As Long Return int1 + int2 End Function End Module
When you run this code, you see this result:
2 + 3 = 5 Press Enter to continue...
Function Syntax
Here's the formal syntax for functions; you use the Function statement:
[ <attrlist> ] [{ Overloads | Overrides | Overridable | NotOverridable | MustOverride | Shadows | Shared }] [{ Public | Protected | Friend | Protected Friend | Private }] Function name[(arglist)] [ As type ] [ Implements interface.definedname ] [ statements ] [ Exit Function ] [ statements ] End Function
The various parts of this statement are the same as for Sub procedures (see the previous topic) except for the As type clause, which specifies the type of the return value from the function. This clause indicates the data type of the value returned by the function. That type can be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, or String, or the name of an enumeration, structure, class, or interface.
The Return statement, if there is one, sets the return value and exits the function; any number of Return statements can appear anywhere in the function, but as soon as one of them is executed, you return from the function to the calling code. You can also use the Exit Function statement to exit the function at any time. If you use Exit Function, how can you return a value from a function? You just assign that value to the function name itself, like this:
Function Summer(ByVal int1 As Integer, ByVal int2 As Integer) As Long Summer = int1 + int2 Exit Function . . . End Function
If you use Exit Function without setting a return value, the function returns the default value appropriate to argtype. That's 0 for Byte, Char, Decimal, Double, Integer, Long, Short, and Single; Nothing for Object, String, and all arrays; False for Boolean; and 1/1/0001 12:00 AM for Date.