2 program example with constants and variables – Rice Lake iRite IDE User Manual

Page 10

Advertising
background image

6

920i

Programming Reference

2.2

Program Example with Constants and Variables

The “Hello, world!” program didn’t use any explicitly declared constants or variables (the string “Hello, world!”
is actually a constant, but not explicitly declared). Most useful programs use many constants and variables. Let’s
look at a program that will calculate the area of a circle for various length radii. The program, named
“PrintCircleAreas”, is listed below.

01

program PrintCircleAreas;

02

03

-- Declare constants and aliases here.

04

g_ciPrinterPort : constant integer := 2;

05

06

-- Declare global variables here.

07

g_iCount : integer := 1;

08

g_rRadius : real;

09

g_rArea : real;

10

g_sPrintText: string;

11

12

13

function CircleArea(rRadius : real) : real;

14

crPie : constant real := 3.141592654;

15

begin

16

-- The area of a circle is defined by: area = pie*(r^2).

17

return (crPie * rRadius * rRadius);

18

end;

19

20

21

begin

22
23

for g_iCount := 1 to 10

24

loop

25

26

g_rRadius := g_iCount;

27

g_rArea := CircleArea(g_rRadius);

28
29

g_sPrintText := "The area of a circle with radius " + RealToString(g_rRadius, 4, 1)

30

+ " is " + RealToString(g_rArea, 7, 2);

31
32

WriteLn(g_ciPrinterPort, g_sPrintText);

33
34

end loop;

35

36

end PrintCircleAreas;

The PrintCircleAreas program demonstrates variables and constants as well as introducing these important ideas:
for loop, assignment statement, function declarations, function calling and return parameters, string
concatenation, WriteLn procedure, a naming convention, comments, and a couple of data conversion functions.

You probably know by now that this program will calculate the areas of circles with radius from 1 to 10
(counting by 1s) and send text like, “

The area of a circle with radius 1 is 3.14

,” once for each radius, out the

communication port 2.

01

program PrintCircleAreas;

Line 1 is the program header with the keyword program and the program identifier “PrintCircleAreas”. This is
the same in theory as the “HelloWorld” program header.

Line 3 is a comment. In

iRite

all comments are started with a -- (double dash). All text after the double dash up

to the end of the line is considered a comment. Comments are used to communicate to any reader what is going
on in the program on the specific lines with the comment or immediately following the comment. The -- can
start on any column in a line and can be after, on the same line, as other valid program statements.

Advertising
This manual is related to the following products: