Function Statement

Defines a procedure that returns a value.

Syntax

Standard form
[ Shared ] Function name [ < typeList > ] [ parameterList ] [ returnType ]
    [ Where genericConstraints ]
    [ statements ]
End Function
Trait aliasing form
[ Shared ] Function name Does traitName . oldName

Parts

Shared
Optional; Specifies that the function is associated with its surrounding object and not an instance of that object.
name
Required identifier
typeList
Optional in the standard form. One or more identifiers separated by commas and enclosed in angle brackets. See Type List for details.
Not present in the aliasing form, even though the procedure may be generic.
parameterList
Optional in the standard form. Zero or more local variables, separated by commas, whose values are assigned when the function is called. See Parameter List for more information.
Not present in the aliasing form, even though the procedure may have parameters
returnType
Optional in the standard form.
Not present in the aliasing form, even though the procedure may have a return type.
Syntax:
As dataType
or
In unit
dataType
Describe
unit
Describe
genericConstraints
Optional. See Where Clause (Generics) for details.
statements
Optional in the standard form.
Not present in the aliasing form.
End Function
Terminates the Function block.
Not present in the aliasing form.
traitName
Required in the aliasing form, the name of a trait that the class or object imports.
oldName
Required in the aliasing form, the name of a method imported through traitName.

Details

Do not confuse the End Function clause with the Exit Function statement.

Defining a function

You can define a function only inside a class, struct, trait, or module. You can not define a function inside another procedure or a loop.

Functions have the private access level. You can call a function only from the same object or module in which it is defined. If you need public access, use a method instead.

You can specify the data type that a function returns. You use either the As or In keyword after the right parenthesis. For an object type, you can not use the syntax "Type #variable". If you do not specify a data type, the type defaults to Real.

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

See also