Declares the name, arguments, and code that define a Function procedure.
Syntax
Function name[(arglist)] [AS type]
[statements]
[Exit Function]
[statements]
End Function
ArgList
The ArgList is made up of one or more arguments, each has the following syntax and parts:
argname1 [AS type][, argname2 [AS type] ...]
Comments
When the Function procedure returns to the calling program, execution continues with the statement following the statement that called it.
The Exit Function statement causes an immediate exit from a Function procedure. A number of Exit Function statements can appear anywhere in the procedure.
To return a value from a function, you can either assign the value to the function name or include it in a Return statement. The following example assigns the return value to the function name MyFunction and then uses the Exit Function statement to return:
Function MyFunction(J AS Number) AS Number
' ...
MyFunction = 3.87
' ...
Exit Function
' ...
End Function
If you use Exit Function without assigning a value to name, the function returns the default value appropriate to argtype. This is 0 for Number; Nothing for Object, String, and all arrays; False for Boolean; and #1/1/0001 12:00 AM# for Date.
The Return statement simultaneously assigns the return value and exits the function, as in the following example:
Function MyFunction(J AS Number) AS Number
' ...
Return 3.87
' ...
End Function
A number of Return statements can appear anywhere in the procedure. You can mix Exit Function and Return statements. You can use a Function procedure on the right side of an expression when you want to use the value returned by the function. You use the Function procedure the same way you use a library function such as Length, Cos, or Chr. You call a Function procedure by using the procedure name, followed by the argument list in parentheses, in an expression. You can omit the parentheses only if you are not supplying arguments. A function can also be called using the Call statement, in which case the return value is ignored.