|
To create a function, you use the Function keyword
followed by a name and parentheses. Unlike a sub procedure, because a
function returns a value, you must specify the type of value the
function will produce. To give this information, on the right side of the
closing parentheses, you can type the As keyword, followed by a data type. To
indicate where a function stops, type End Function. Based on this, the
minimum syntax used to create a function is:
AccessModifier(s) Function FunctionName() As DataType
End Function
As seen for a sub procedure, a function can have an
access modifier. The rules are the same as we described for a sub
procedure.
The Function keyword is required.
The name of a function follows the same rules and
suggestions we reviewed for sub procedures.
The As keyword may be required (in the next
sections, we will review the alternatives to the As DataType
expression).
The DataType factor
indicates the type of value that the function will return. If the function
will produce a word or a group of words, you can create it as String. The other data types are
also valid in the contexts we reviewed them. Here is an example:
Function GetFullName() As String
End Function
As done with the variables, you can also use a type
character as the return type of a function and omit the As DataType
expression. The type character is typed on the right side of the function
name and before the opening parenthesis. An example would be GetFullname$().
As with the variables, you must use the appropriate type character for the
function:
| Character |
The function must return |
| $ |
a String type |
| % |
a Byte, Short, Int16, or In32 |
| & |
an Int64 or a Long |
| ! |
a Single type |
| # |
a Double |
| @ |
a Long integer |
Here is an example:
Function GetFullName$()
End Function
As mentioned already, the section between the Function
and the End Function lines is the body of the function. It is used to
describe what the function does. As done on a sub procedure, one of the
actions you can perform in a function is to declare a (local) variable and
use it as you see fit. Here is an example:
Function CallMe() As String
Dim Salute As String
Salute = "You can call me Al"
End Function
|
Returning a Value From a Function
|
|
After performing an assignment in a function, to
indicate the value it returns, somewhere after the assignment and before
the End Function line, you can type the name of the function, followed by the =
sign, followed by the value the function returns. Here is an example in
which a function returns a name:
Function GetFullName$()
Dim FirstName As String, LastName As String
FirstName = InputBox("Enter First Name: ")
LastName = InputBox("Enter Last Name: ")
GetFullName = LastName & ", " & FirstName
End Function
Alternatively, instead of using
the name of the function to indicate the value it returns, you can type Return,
followed by the value or the expression that the function returns. Based
on this, the above function could also be created as follows:
Function GetFullName$()
Dim FirstName As String, LastName As String
FirstName = InputBox("Enter First Name: ")
LastName = InputBox("Enter Last Name: ")
Return LastName & ", " & FirstName
End Function
You can also use some local variables in the function
to perform an assignment and then assign their result to the name of the
function.
|
Practical Learning: Creating a Function
|
|
- To add a project to the current solution, on the main menu, click
File -> Add -> New Project
- Set the Name to Rectangular and click OK
- In the Solution Explorer, under Rectangular, right-click Module1 and
click Rename
- Type Rectangle.vb and press Enter
- To create a function, change the file as follows:
Public Module Rectangle
Private Function CalculatePerimeter() As Double
Dim dblLength As Double
Dim dblWidth As Double
dblLength = InputBox("Enter Rectangle Length: ")
dblWidth = InputBox("Enter Rectangle Width: ")
CalculatePerimeter = (dblLength + dblWidth) * 2
End Function
Public Sub Main()
End Sub
End Module
|
- Save all
As done for the sub procedure, in order to use a function in your program,
you must call it. Like a sub procedure, to call a function, you can simply type
its name in the desired section of the program. Here is an example:
Sub Main()
CallMe
End Sub
Since the primary purpose of a function is to return a value, to better
take advantage of such a value, you can assign the name of a function to a
variable in the section where you are calling the function.
Here is an example:
Module Exercise
Function GetFullName$()
Dim FirstName As String, LastName As String
FirstName = InputBox("Enter First Name: ")
LastName = InputBox("Enter Last Name: ")
Return LastName & ", " & FirstName
End Function
Friend Sub Main()
Dim FullName$
FullName = GetFullName()
MsgBox(FullName)
End Sub
End Module
Here is an example of running this program:



|