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. 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
or
In 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 as an alternative.

You can specify the data type that a function returns. You use one of the keywords As or In after the right parenthesis. For an object type, you cannot use the syntax "Type #variable". If you do not specify a data type, the default type is Int.

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

See also