Function Statement
Language Items List
Definition:
Declares and defines the name, arguments, and code that form the body of a
Function procedure that returns a specified data type.
Syntax:
[Static]Function functionname [(argumentlist)] [As type]
[statementblock]
[functionname = expression]
[Exit Function]
End Function
Details:
All executable code must be entered in the Methods Window as Function or Sub
procedures. A Function procedure cannot be nested within another Function or Sub
procedure.
Syntax Description
Static Indicates that the Function procedure's local variables do not get reset
between calls.
Function Indicates the beginning of a Function procedure.
functionname The function's name. When naming Function procedures follow the naming
conventions used for naming other variables. A type-declaration character can be
included.
The data type returned by the Function procedure is determined by the data
type of the name. To specify the Function procedure's return data type, simply use
a type-declaration character in the Function name or place an As clause after argumentlist. For example, the following statements do the same thing:
Function ReturnString$()
Function ReturnString() As String
argumentlist List of variables that represent arguments passed to the called Function
procedure. Separate multiple variables with commas. Individual arguments are passed
by reference, unless identified using ByVal. This means that changing an
argument’s value inside the Function procedure also changes its value in the calling
procedure. If an argument passed to a function is an expression, no part of the
expression is changed by the function.
As type Phoenix reserved word that declares the data type of the Function procedure's
return value. The data type can be Integer, Long, Single, Double, Currency,
String, or the name of an object.
statementblock One or more program lines located within the body of the Function procedure.
These lines make up the body of the function that is executed.
expression The Function’s return value. A Function procedure returns the value assigned to the
function functionname. You can place any number of these functionname assignments anywhere within the procedure. If a value is not assigned to functionname, the procedure defaults to a 0 return value for a numeric function, a
zero-length string (“”) return value for a String function, and Nothing for object return types
(object references).
Exit Function Immediatly exits the Function procedure. Program execution continues with the
statement following the one that called the Function procedure. You can include
as many Exit Function statements as you like in a Function procedure.
End Function Indicates the end of a Function procedure.
The argument argumentlist has the following syntax:
[ByVal]variable[()] [,[ByVal]variable [()] ] [ As type] ...
Syntax Description
ByVal Specifies that the argument is passed by value, not by reference. Do not use
ByVal of an object type, or a variable that is an array.
Name of the variable that represents the argument. For array variables, omit
the number of dimensions and include the parentheses.
As type Declares the data type of variable as Double, Currency, Integer, Long, Single, String (variable-length strings
only) or object type. A separate As type clause should be used for each set of variables.
The following examples show various ways to declare integers. The following
line declares two by-reference integer arguments:
x,y As Integer
To declare one ByVal integer argument and one by-reference integer argugment,
use the following:
ByVal arg1, arg2 As Integer
The following line declares two by-reference integer arguments, and one
by-reference single argument:
x,y As Integer, z As Single
To declare a single argument of type object reference to objects of type Form,
use the following line:
f As Form
Trying to pass any object that is not a Form or does not inherit from Form
throws a type-mismatch program exception. For example, declaring an argument type
to be “Window” would allow objects of type Window, Control, Form, and others under each of
those types, but not unrelated types.
A Function procedure is an individual procedure that can take arguments,
perform a series of statements, and change its argument values just like a Sub
procedure. However, a Function procedure differs from a Sub procedure in the way it
is handled in an expression. A Sub procedure can be used in an expression like
any intrinsic function, such as Sqr, Cos, or Chr. To call a Function procedure
in an expression, you must use the functionname, followed by the argumentlist enclosed in parentheses. Parentheses are not required if there is no argumentlist.
Caution: Function procedures can be recursive, meaning that they can call themselves to
perform a specified task. Recursive procedures, however, can cause a stack
overflow exception to be thrown. The Phoenix reserved word Static is usually not
used with recursive Function procedures, because a unique set of local variables
is usually desired when making a recursive function call. Static variables
would be the same for each invocation of the function.
Any time the name of the function appears in an expression, a call will be
made. For example: If functioname = False Then ... The intent of this line may have been to test the current
return value of the function, but this value is not accessible, instead, the
function is called making it recursive.
To return a value from a function, you must assign the value to the function
name. For example, in a function named GetBitmap().
Function GetBitmap(...) As Bitmap
...
GetBitmap = Image.Picture
...
End Function
Variables used in Function procedures must be explicitly declared using Dim or
the equivalent within the particular procedure. These variables are always
local to the procedure.
See Also:
Dim Statement
Static Statement
Sub Statement