Cicode Programming Reference > Writing Functions | Cicode

Writing Functions

Citect SCADA is supplied with over 600 built-in functions. One of these functions (or several functions in combination) can usually perform the required tasks in your system. However, where system functionality cannot be achieved with built-in functions, you can write your own functions.

A Cicode function is a small program: a collection of statements, variables, operators, conditional executors, and other functions.

While it is not necessary to be an experienced programmer to write simple Cicode functions, it is strongly recommended not to attempt to write large, complex functions unless you are familiar with computer programming, and have experience with Cicode. Functions are equivalent to the subroutines of BASIC and assembly language, and the subroutines and functions used in Pascal and C.

Cicode functions can have many purposes. Quite often, functions are used to store a common set of commands or statements that would otherwise require repetitious typing and messy command or expression fields.

Some functions are simple, created to avoid a long command or expression. For example, the following command increments the variable tag COUNTER:

Command

IF COUNTER < 100 THEN COUNTER = COUNTER + 1; ELSE COUNTER = 0; END;

This command would be easier to use (and re-use) if it was written as a function that can be called in the command:

Command

IncCounter ( );

To be able to use the function like this, you need to write it in a Cicode file, and declare it with the FUNCTION keyword:

FUNCTION
IncCounter ( )
IF COUNTER < 100 THEN
COUNTER = COUNTER + 1;
ELSE
COUNTER = 0;
END
END

Be aware that the indented code is identical in functionality to the long command above.

By placing the command code inside a function, and using the function name in the command field as in the previous example, this function need only to be typed once. It can then be called any number of times, from anywhere in Citect SCADA that requires this functionality. Because the code exists in the one location, rather than repeated wherever needed (in potentially many places), it can be easily maintained (altered if necessary).

Note: The Cicode Editor Editor is designed specifically for editing and debugging Cicode functions.

See Also

Published June 2018