beautypg.com

Function, exitfunction, end function, Endfunction – Campbell Scientific CR9000X Measurement and Control System User Manual

Page 177

background image

Section 5. Program Declarations

FUNCTION, EXITFUNCTION, END FUNCTION

Declares the name, variables, and code that form a user defined Function.

Syntax

Function

FunctionName [(Optional VariableList )] As DataType

[

DIM ] Declare local variables, Optional

[

statementblock ]

[

Return (expression) ]

[

ExitFunction ]

Optional

[

statementblock ]

EndFunction

Remarks

Functions with their parameters are called just like built in functions; i.e., by
simply using their name with parameters anywhere within an expression (see
example below). When calling a function, closing parenthesis must be used
even if the function has no parameters. The parenthesis indicate a call to the
function. If parenthesis are omitted, the last value returned by the function is
used rather than the function running again. One difference between a Sub and
a Function is a Function returns a value, whereas a subroutine does not. By
default, the Function value returned is a Float, but it can be specified as a
String (with an optional * size), Long, or Boolean in the Function routine by
using the AS Datatype after the Function Name (and parameters if used) of the
Function Declaration (example: "Function Name(parameters) AS Long").

Functions can be nested a maximum of two deep. If a function declaration

contains a call to another function, which in turn contains a call to a function, a

compiler error is returned. Only one instance of a function can run at any given

time.

A Function call includes the ability to pass in optional parameters. As with a
subroutine declaration, the Function routine parameter list describes local
parameters and optionally their type (Float, Long, Boolean, String). If not
specified, the default parameter type is Float. The number and sequence of the
program variables/values in the Function call must match the number and
sequence of the variable list in the Function declaration. The Function call
parameter values are copied into the Function's local parameter list. Unlike a
Subroutine Call, even if the local variables are modified in the Function
routine, these changes are not passed back to the Function call parameter
variables.

5-7