12.1.3 Program Structure

Scripts are divided into two parts: Declaration Part and Program Part. The declaration part is for the declaration of internal variables and establishing tags as variables. The program part includes all program statements except for the variable declarations.

Comments can be entered anywhere in the program. Strings starting with ‘//’ are treated as comments until the end of the line.

 (1) Declaration Part

Item

Description

Variable Declaration

Declare internal variables using the following format:

VAR variable_name;

The initial value of declared variables is 0. To declare more than one variable, use a comma “ , ” between variables:

VAR variable_name_1, variable_name_2,, variable_name_n;

You can use multiple VAR keywords before the program part starts.

Variable names cannot share the same names as tag names (Group, Digital, Analog, or String tags). Duplicate names may cause compiler errors or incorrect operation.

Input Parameter Declaration

Declare input parameters using the following format:

PARAM 1st parameter [, 2nd parameter …];

Input parameters are used when a program is called by another program or command. The declaration order must be identical to the order of input parameters. The parameters will be initialized with the values assigned by the caller.

To declare more than one parameter, use a comma “ , ” between them. You can use several PARAM keywords before the program part starts.

Example

//Program Name: MyPgm

VAR a, b;

VAR c;

PARAM p1, p2;

PARAM p3;

 

The program MyPgm can be called by the external caller as shown below:

MyPgm(1,2,3);

The values (1, 2, and 3) will be stored in the parameters p1, p2, and p3, respectively. The variables a, b, and c, will be initialized as 0.

 (2) Program part

In the program part, all program statements except the declaration (variables and parameters) can be used. An example of the most basic statement is the function call, calculation, and storing the result.

The following program example is composed of basic statements.

Tag_a = Tag_a + 1;

Tag_b = MyPgm(Tag_a, 2, 3);

Each statement must end with a semicolon “ ; ”. Each function returns a single result to the caller. The program shown above returns the result of MyPgm to “Tag_b”.

(3) Constants

When you use constants in the program, you can use the notations as shown below.

Item

Description

Octal Constants

Base 8; digits only range from 0 to 7. Octal constants must start with 0. (E.g. 01277)

Decimal Constants

The general notation is used. (E.g. 153, 3.14, 2.45E-12)

Hexadecimal Constants

Base 16. Must start with ‘0x’. Use the alphabet or number characters between ‘0’ and ‘F’. (E.g. 0xFFFF)

String Constants

Strings must be between two double-quotation marks (“ ”). (E.g. “String Variable1”)