Declares the name, arguments, and code that form the body of a Function procedure.
Function name ([arglist]) [As type] [statements] [name = expression] [Exit Function] [statements] [name = expression] End Function |
The Function statement syntax has these parts:
Part | Description |
name | Required. Name of the Function; follows standard variable naming conventions. |
arglist | Optional. List of variables representing arguments that are passed to the Function procedure when it is called. Multiple variables are separated by commas. |
type | Optional. Data type of the value returned by the Function procedure; may be Byte, Boolean, Integer, Long, Single, Double, Date, String (except fixed length), Object , Variant or an object type. |
statements | Optional. Any group of statements to be executed within the Function procedure. |
expression | Optional. Return value of the Function. |
The arglist argument has the following syntax and parts:
[ByVal | ByRef] varname [As type] [=defval] |
Part | Description |
ByVal | Optional. Indicates that the argument is passed by value. ByVal is the default in ConceptDraw Basic. |
ByRef | Optional. Indicates that the argument is passed by reference. |
varname | Required. Name of the variable representing the argument being passed to the procedure; follows standard variable naming conventions. |
type | Optional. Data type of the argument passed to the procedure; may be Byte, Boolean, Integer, Long, Single, Double, Date, String (variable length only), Object, Variant or an object type. |
defval | Optional. Constant that determine the value that will be passed to the procedure by default if this argument is omitted. |
Function procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow.
The Exit Function statement causes an immediate exit from a Function procedure. Program execution continues with the statement following the statement that called the Function procedure. Any number of Exit Function statements can appear anywhere in a Function procedure.
Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. However, unlike a Sub procedure, you can use a Function procedure on the right side of an expression in the same way you use any intrinsic function, such as Sqr, Cos, or Chr, when you want to use the value returned by the function.
You call a Function procedure using the function name, followed by the argument list in parentheses, in an expression. See the Call statement for specific information on how to call Function procedures.
To return a value from a function, assign the value to the function name. Any number of such assignments can appear anywhere within the procedure. If no value is assigned to name, the procedure returns a default value: a numeric function returns 0, a string function returns a zero-length string (""), and a Variant function returns Empty. A function that returns an object reference returns Nothing if no object reference is assigned to name (using Set) within the Function.
Variables used in Function procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure.
This example uses the Function statement to declare the name, arguments, and code that form the body of a Function procedure.
' The following user-defined function returns the square root of the ' argument passed to it. Function CalculateSquareRoot(NumberArg As Double) As Double If NumberArg < 0 Then ' Evaluate argument. Exit Function ' Exit to calling procedure. Else CalculateSquareRoot = Sqr(NumberArg) ' Returns square root. End If End Function |
See Also |
Call Statement , Dim Statement , Exit Statement , Sub Statement |