Rice Lake iRite IDE User Manual

Page 11

Advertising
background image

920i

Programming Reference - Tutorial

7

Line 4 is a global constant declaration for the communication port that a printer may be connected to. This simple
line has many important parts:

04

g_ciPrinterPort : constant integer := 2;

First, an identifier name is given. Identifier names are made up by the programmer and should accurately
describe what the identifier is used for. In the name g_ciPrinterPort the “PrinterPort” part tells us that this
identifier will hold the value of a port where a printer should be connected. The “g_ci” is a prefix used to
describe the type of the identifier. When “g_ciPrinterPort” is used later on in the program, the prefix may help
someone reading the program, even the program’s author, to easily determine the identifier’s data type without
having to look back at the declaration.

The “g_” in the prefix helps tell us that the identifier is “global”. Global identifiers are declared outside of any
subprogram (handler, function, procedure) and have global scope. The term “scope” refers to the region of the
program text in which the identifier is known and understood. The term “global” means that the identifier is
“visible” or “known” everywhere in the program. Global identifiers can be used within an event handler body, or
any procedure or function body. Global identifiers also have “program duration”. The duration of an identifier
refers to when or at what point in the program the identifier is understood, and when their memory is allocated
and freed. Identifiers with global duration, in a

920i

program, are understood in all text regions of the program,

and their memory is allocated at program start-up and is re-allocated when the indicator is powered up.

The “c” in the prefix helps us recognize that the identifier is a constant. Constants are a special type of identifier
that are initialized to a specific value in the declaration and may not be changed anytime or anywhere in the
program. Constants are declared by adding the keyword constant before the type.

Constants are very useful and make the program more understandable. In this example, we defined the printer
port as port 2. If we would have just used the number 2 in the call to WriteLn, then a reader of the program would
not have any idea that the programmer intended a printer to be connected to the

920i

’s port 2.

Also, in a larger program, port 2 may be used hundreds of times in Write and WriteLn calls. Then, if it were
decided to change the printer port from port 2 to port 3, hundreds of changes would have to be made. With port 2
being a constant, only one change in the declaration of g_ciPrinterPort would be required to change the printer
port from 2 to 3.

The type of the constant is an integer. The “i” in the prefix helps us identify g_ciPrinterPort as an integer. The
keyword integer follows the keyword constant and specifies the type compatibility of the identifier as an integer
and also determines how much memory will be required to store the value (a value of 2 in this example). In the

iRite

programming language, there are only 3 basic data types: integer, real and string.

The initialization of the constant is accomplished with the “:= 2” part of the statement. Initialization of constants
is done in the declaration, with the assignment operator, :=, followed by the initial value.

Finally, the statement is terminated by a semicolon. The “;” is used in

iRite

and other languages as a statement

terminator and separator. Every statement must be terminated with a semicolon. Don’t read this to mean “every
line must end in a semicolon”; this is not true. A statement may be written on one line, but it is usually easier to
read if the statement is broken down into enough lines to make some keywords stand out and to keep the length
of each line less than 80 characters.

Some statements contain one or more other statements. In our example, the statement:

g_ciPrinterPort : constant integer := 2;

is an example of a simple statement that easily fit on one line of code. The loop statement in the program startup
handler (main code body) is spread out over several lines and contains many additional statements. It does,
however, end with line end loop;, and ends in a semicolon.

06

-- Declare global variables here.

07

g_iCount : integer := 1;

08

g_rRadius : real;

09

g_rArea : real;

10

g_sPrintText: string;

Line 6 is another comment to let us know that the global variables are going to be declared.

Advertising
This manual is related to the following products: