Function
Statement
Makes a private procedure that returns a value.
Syntax
Usual construct
[ modifiers ] _
Function function_name [ type_list ] [ parameter_list ] [ return_type ]
[ Where generic_constraints ]
[ statements ]
End [ Function ]
Name-change construct
[ modifiers ] _
Function function_name Does trait_name . supplied_name
Parts
modifiers
- Optional
@Iterator
– The procedure implements the iterator pattern. See @Iterator.@Shared
– only procedures in the same scope that are also@Shared
can call the procedure.
function_name
- Mandatory name for the function.
type_list
- Optional one or more names with a comma between each, all between brackets (
[ ]
). See Type List for more information. - Not permitted in the name-change 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-change construct.
return_type
- Optional if
function_name
has a type character. See Type Characters for more information. - Mandatory if
function_name
does not have a type character. - Not permitted in the name-change 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 statements to declare elements and/or run some code.
- Not permitted in the name-change 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-change construct.
trait_name
- In the name-change construct, mandatory name of a trait that the class or object applies.
supplied_name
- In the name-change construct, mandatory name of a procedure supplied by a trait.
Instructions
End Function
is not the same as Exit Function
.
A function is a type of procedure that has many limits. It is at the core of functional programming. These limits include:
- Its access level is private. Only procedures in the same container can call it. External code cannot directly call a function.
- A function is constant.
It does not have access to and cannot change non-local state.
Thus you cannot use the modifier
@Shared
on variables in a function. And you cannot use the modifierByRef
on parameters. - Code in a function can call only constant procedures.
Constant procedures include other functions and procedures with the modifier
@Const
. - A function must Return a value.
A function has the qualities that follow:
- It can run at compile-time.
- It lets the compiler make better machine code.
- It lets you do concurrency more safely.
Defining a function
You can put a function only in a class, structure, 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, 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 (examples are i%
, r!
, or s$
).
If you do not specify a data type, this will cause an error.
Returning from a function
Function Sum(a As Real64, b As Real64) As Real64
Return a + b
End Function
Function Sum(a As Real64, b As Real64) As Real64
Sum = a + b
Exit Function
End Function
Calling a function
Dim result As Real64
result = Sum(14.0, 28.0)
Examples
TODO