Function/EndFunction (Create a Function)

The Function/EndFunction declaration is used to create a user defined function.

Syntax

Function FunctionName Optional (optional parameters) optional As DataType

Return ( expression ) (optional)

ExitFunction (optional)

EndFunction

Calls to a Function

Variable = FunctionName( ) 'Call function

Variable = FunctionName 'returns last value from function

Remarks

The Function/EndFunction declaration is similar to a subroutine declaration (Sub/EndSub). By default, a Function is a ClosedFloat Four-byte floating-point data type. Default datalogger data type for Public or Dim variables. Same format as IEEE4., but it can be specified as a ClosedString A data type used when declaring a variable consisting of alphanumeric characters. (with an optional * size), ClosedLong Data type used when declaring a variable as an integer., or ClosedBoolean Data type used to represent conditions or hardware that have only two states (true or false) such as flags and control ports.. As with a subroutine declaration, the parameter list describes local parameters and optionally their type (Float, Long, Boolean, String). If not specified, the default parameter type is Float. One difference between a Sub and a Function is a Function returns a value, whereas a subroutine does not. Another difference is if a value being passed into a function is an array, only the first value, or the value defined by the index, is used (that is, the entire array is not passed into the function). Strings are terminated with a null character; thus, a string passed into a function will include only those characters up until a null character is encountered. The FunctionName parameter is limited to 39 characters.

Function names with their parameters are called just like built in functions; i.e., by simply using their name with parameters anywhere within an expression. When a function is called, the parameters are copied into the Function's local parameter list, as is the case when subroutines are called. However, unlike subroutines, which when exited copy the local parameter values back out to any variables that were passed in, Functions do no such copying back out but rather return a value that is used by the expression that invoked the function. Only one instance of a function can run at any given time.

In order to call a Function, parentheses must be used even if the function has no parameters. The parentheses indicate a call to the function. When a function is called within the function itself and its parentheses are omitted, the last value returned by the function is used rather than the function running again. That is, when no parentheses are used, the function behaves like a variable that holds the last value returned by the function (see program example 4). Note that like variables, Functions are initialized to zero.

Return or ExitFunction exits the function at run time. EndFunction terminates the Function declaration and exits the function at run time. The expression returned by the Function is specified by Return(expression), or, if Return is not executed before EndFunction or ExitFunction, then the return value is specified by the assignment of an expression to the Function's name. If neither of these methods are used, then NAN is returned.

Functions include the ability to pass in optional parameters. These optional parameters are preceded by the Optional keyword. Optional parameters must be initialized using a constant value (required parameters cannot be initialized). The default for an optional parameter is used by passing in a comma; multiple default parameters are specified using multiple commas. Required parameters cannot follow optional parameters in the definition of the function.

Functions can be nested to a maximum of five deep. If a function is nested more than five deep, when the program is sent to the datalogger, a warning will be given that the function will not be executed because it is nested too deep.

Variables declared by Dim within a subroutine or function are local to that subroutine or function. The same variable name can be used within other subroutines or functions or as a global variable without conflict. Variables used as parameters to a subroutine or function are also local. Note that variables initialized within a subroutine or function are initialized only at compile time. They are not re-initialized with each call to the subroutine or function.

Pointer variables can be used in functions to pass in arguments based on memory location rather than variable name.