Function Statement

Defines a procedure that returns a value.

Syntax

Usual construction

[ [Shared] ] Function function_name [ <type_list> ] [ parameter_list ] [ return_type ]
    [ Where generic_constraints ]
    [ statements ]
End [ Function ]

Name-replacement construction

[ Shared ] Function function_name Does trait_name . old_name

Parts

Shared
Optional – only procedures at the same level that are also Shared can call the procedure.
function_name
Necessary name
type_list
Optional in the usual construction. See Type List for more information.
Not permitted in the name-replacement construction.
parameter_list
Optional in the usual construction. One or more local variables, with a comma between each one. The caller gives them their values. See Parameter List for more information.
Not permitted in the name-replacement construction.
return_type
Optional in the usual construction.
Not permitted in the name-replacement construction.

Syntax

As type
or
In unit
type
Describe
unit
Describe
generic_constraints
Optional. See Where Clause (Generics) for more information.
statements
Optional in the usual construction.
Not permitted in the name-replacement construction.
End
Completes the statement. You can also use End Function.
Not permitted in the name-replacement construction.
trait_name
Necessary in the name-replacement construction – the name of a trait that the class or object applies.
old_name
Necessary in the name-replacement construction – the name of a procedure supplied by a trait.

Instructions

Do not confuse End Function with the statement Exit Function.

Defining a function

You can define a function only inside a class, struct, trait, or module. You cannot 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 cannot 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