HP (Hewlett-Packard) NW280AAABA Calculator User Manual


 
Programming 513
different values. You do likewise to declare a local
variable in a program: specify the name of the program,
followed by the dot, and then variable name.
Functions,
their
arguments,
and
parameters
You can define your own functions in a program, and data
can be passed to a function using parameters. Functions
can return a value (using the RETURN statement) or not.
When a program is executed from Home view, the
program will return the value returned by the last statement
that was executed.
Furthermore, functions can be defined in a program and
exported for use by other programs, in much the same
way that variables can be defined and used elsewhere.
In this section, we will create a small set of programs, each
illustrating some aspect of programming in the HP Prime.
Each program will be used as a building block for a
custom app described in the next section, App Programs.
Program ROLLDIE We’ll first create a program called ROLLDIE. It simulates
the rolling of a single die, returning a random integer
between 1 and whatever number is passed into the
function.
In the Program Catalog create a new program named
ROLLDIE. (For help, see page 501.) Then enter the code
in the Program Editor.
EXPORT ROLLDIE(N)
BEGIN
RETURN 1+FLOOR(RANDOM(N));
END;
The first line is the heading of the function. Execution of the
RETURN statement causes a random integer from 1 to N
to be calculated and returned as the result of the function.
Note that the RETURN command causes the execution of
the function to terminate. Thus any statements between the
RETURN statement and END are ignored.
In Home view (in fact, anywhere in the calculator where a
number can be used), you can enter ROLLDIE(6) and a
random integer between 1 and 6 inclusive will be
returned.