Like a sub procedure, a
function is used to perform an assignment. The main difference between a sub
procedure and
a function is that, after carrying its assignment, a function gives back a
result. We also say that a function "returns a value". To
distinguish both, there is a different syntax you use for a function.
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 should/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:
Function FunctionName() As DataType
End Function
The name of a function follows the same rules and
suggestions we reviewed for sub procedures. 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. If
the function will check something and determine whether it produce a true
or a false value, you can create it as Boolean. The other data types are
also valid in the contexts we reviewed them. Here is an example:
Module Module1
Function GetFullName() As String
End Function
End Module
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 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:
Module Module1
Function GetFullName$()
End Function
End Module
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
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:
Module Module1
Function GetFullName$()
Dim FirstName, LastName As String
Console.Write("Enter First Name: ")
FirstName = Console.ReadLine()
Console.Write("Enter Last Name: ")
LastName = Console.ReadLine()
GetFullName = LastName & ", " & FirstName
End Function
End Module
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:
Module Module1
Function GetFullName$()
Dim FirstName, LastName As String
Console.Write("Enter First Name: ")
FirstName = Console.ReadLine()
Console.Write("Enter Last Name: ")
LastName = Console.ReadLine()
Return LastName & ", " & FirstName
End Function
End Module
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.
|