Function
Statement
Makes a private procedure that returns a value.
Syntax
Usual construct
[ @Shared ] Function function_name [ type_list ] [ parameter_list ] [ return_type ]
[ Where generic_constraints ]
[ statements ]
End [ Function ]
Name-replacement construct
[ @Shared ] Function function_name Does trait_name . supplied_name
Parts
@Shared
- Optional – only procedures in the same scope that are also
@Shared
can call the procedure. function_name
- Mandatory name for the function.
type_list
- Optional in the usual construct. See Type List for more information.
- Not permitted in the name-replacement construct.
parameter_list
- Optional in the usual construct.
One or more local variables, with a comma between each.
The caller gives them their values.
The modifier
ByRef
is not permitted. See Parameter List for more information. - Not permitted in the name-replacement construct.
return_type
- Optional in the usual construct.
- Not permitted in the name-replacement construct.
-
Return type
As type
orIn unit
type
- A data type
unit
- A unit of measure
generic_constraints
- Optional. See Where Clause (Generics) for more information.
statements
- In the usual construct, optional one or more declarative or executable statements.
- Not permitted in the name-replacement construct.
End
- Completes the statement.
You can also use
End Function
. - You can change this part of the syntax. See @Option Directive for more information.
- Not permitted in the name-replacement construct.
trait_name
- In the name-replacement construct, mandatory name of a trait that the class or object applies.
supplied_name
- In the name-replacement construct, mandatory name of a procedure supplied by a trait.
Instructions
End Function
is not the same as Exit Function
.
Defining a function
You can put a function only in a class, struct, trait, or module. You cannot put a function in a different procedure, or in a loop.
Functions have the private access level. You can call a function only from the same object or module in which it is declared. If public access is necessary, then use a method.
You must specify the data type that a function returns.
You usually use one of the keywords As
or In
after the right parenthesis.
Alternatively, you can supply a name with a type character (i%
, r!
, or s$
).
If you do not specify a data type, then this will cause an error.
Returning from a function
Function Sum(a As Real, b As Real) As Real
Return a + b
End Function
Function Sum(a As Real, b As Real) As Real
Sum = a + b
Exit Function
End Function
Calling a function
Dim result As Real
result = Sum(14.0, 28.0)
Examples
TODO