Rice Lake iRite IDE User Manual

Page 12

Advertising
background image

8

920i

Programming Reference

Lines 7—10 are global variable declarations. One integer, g_iCounter, two reals, g_rRadius and g_rArea, and
one string, g_sPrintText, are needed during the execution of this program. Like the constant g_ciPrinterPort,
these identifiers are global in scope and duration; however, they are not constants. They may have an optional
initial value assigned to them, but it is not required. Their value may be changed any time they are “in scope”,
they may be changed in every region of the program anytime the program is loaded in the 920i.

Lines 13—18 are our first look at a function declaration. A function is a subprogram that can be invoked (or
called) by other subprograms. In the PrintCircleAreas program, the function CircleArea is invoked in the
program startup event handler. The radius of a circle is passed into the function when it is invoked. In

iRite

there

are three types of subprograms: functions, procedures, and handlers.

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;

On line 13, the function declaration starts with the keyword function followed by the function name. The
function name is an identifier chosen by the programmer. We chose the name “CircleArea” for this function
because the name tells us that we are going to return the area of a circle. Our function CircleArea has an optional
formal arguments (or parameters) list. The formal argument list is enclosed in parenthesis, like this: (rRadius
: real)

. Our example has one argument, but functions and procedures may have zero or more.

Argument declarations must be separated by a semicolon. Each argument is declared just like any other variable
declaration: starting with an identifier followed by a colon followed by the data type. The exception is that no
initialization is allowed. Initialization wouldn’t make sense, since a value is passed into the formal argument
each time the function is called (invoked).

The rRadius parameters are passed by value. This means that the radius value in the call is copied in rRadius. If
rRadius is changed, there is no effect on the value passed into the function. Unlike procedures, functions may
return a value. Our function CircleArea returns the area of a circle. The area is a real number. The data type of the
value returned is specified after the optional formal argument list. The type is separated with a colon, just like in
other variable declarations, and terminated with a semicolon.

Up to this point in our program, we have only encountered global declarations. On line 14 we have a local
declaration. A local declaration is made inside a subprogram and its scope and duration are limited. So the
declaration: crPie : constant real := 3.141592654; on line 14 declares a constant real named
crPie with a value of 3.141592654. The identifier crPie is only known—and only has meaning—inside the text
body of the function CircleArea. The memory for crPie is initialized to the value 3.141592654 each time the
function is called.

Line 15 contains the keyword begin and signals the start of the function code body. A function code body
contains one or more statements.

Line 16 is a comment that explains what we are about to do in line 17. Comments are skipped over by the
compiler, and are not considered part of the code. This doesn’t mean they are not necessary; they are, but are not
required by the compiler.

Every function must return a value. The value returned must be compatible with the return type declared on line
14. The keyword return followed by a value, is used to return a value and end execution of the function. The
return statement is always the last statement a function runs before returning. A function may have more than
one return statement, one in each conditional execution path; however, it is good programming practice to have
only one return statement per function and use a temporary variable to hold the value of different possible return
values.

The function code body, or statement lists, is terminated with the end keyword on line 18.

In this program we do all the work in the program startup handler. We start this unnamed handler with the begin
keyword on line 21.

23

for g_iCount := 1 to 10

24

loop

Advertising
This manual is related to the following products: