Cicode Programming Reference > Writing Functions > Function Uses

Function Uses

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 CitectSCADA 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).