
Basic Programming Functions and Operators
:=
All programs in Derive 5 are functions. Functions
are declared using the
:=
operator. Functions
are written and edited in the Expression Entry Toolbar.
It is also possible to define functions through the Declare > Function
Definition command. The Declare > Function Definition
command has the advantage that the function name and arguments are on a separate line than
the definition. Also, any existing function definition can be displayed and edited
by clicking on the pull down menu in the Function Definition: field.
Lets write a very simple program. This
program just squares any number.
Start Derive and, ensuring that the cursor is
flashing in the expression entry line, type
Square(x):=x^2 and press Enter. Then
#1: Square(x)
:= x2
should appear in
the algebra window.
Notice that function names can be written and displayed in lower and mixed
case. Before version 5 this could only be done by switching to case-sensitive input
mode.
To use this function to find the square of 6,
enter Square(6)
in the expression entry line and simplify or approximate this. Alternatively, type
Square(6)
in
the expression entry line and press Ctrl + Enter or the
button to the left of the entry line.
PROG(arg1,arg2, ... ,
argn)
This is a fundamental control construct
for programming,
PROG()
executes all of the arguments arg1, arg2,
,
argn
in turn and returns the outcome of argn to the algebra window (if indeed it should reach and
be able to evaluate argn).
For example PROG(x:=1,y:=2,x^2+y^2),
assigns the value 1 to the variable x, then assigns the value 2 to the variable y, then
computes the value of
x2+y2 and returns this value to the algebra window.
Also, if a PROG() command
is simplified (and is not part of a function definition) the assignments within the PROG() are NOT applied locally. That is, if variables have been assigned values
within a PROG(), those variables are
still assigned to those values after the PROG()
has terminated . In
other words, when the above PROG() has terminated
x
and y are assigned the values of 1 and 2, respectively, in
the current algebra window.
In the next section we show
how to assign variables LOCALLY, that is the variables are only assigned
these values during the duration of the PROG().
PROG() is normally used in a function
definition and when this is so the arguments of
PROG() are
displayed in a 2 dimensional indented form. For
example the function
FOO(x,y)
computes the value of
(x+y)(x-y).
FOO(x,
y) :=
Prog
c := x + y
d := x - y
c·d |
as you can see the display is in 2
dimensional format and the arguments of the PROG() are
indented. This feature will become invaluable
for debugging and unravelling function definitions.
The above program would be entered
in the expression entry line as
FOO(x,
y) := PROG(c := x + y, d := x - y, c·d)
In actual fact the the
function definition above was sloppily written, because I
did not declare the variables
c and
d to be local variables.
Variables are declared local by adding them to the
arguments of the function definition. In this particular example there were no ill
effects. But it is good practice (and
courteous) to declare all the variables that are used in a program. So,
FOO(x,y) should have been entered as
FOO(x,y,c,d) := PROG(c := x + y, d := x - y, c·d)
We will look at a slight modification of
FOO(x,y) to see
how we can become unstuck by not declaring local variables.
FOO(x,
y) :=
Prog
c1 := x + y
c2 := x - y
c·1·c·2 |
Because
c1 and
c2 are not
declared as local variables, the product of c1 and
c2 is interpreted as the product of c,
1,
c and
2. Not the result we were after! Below is how the function should have been
declared.
FOO(x, y, c1, c2) := Prog
c1 := x + y
c2 := x - y
c1·c2 |
LOOP(arg1,arg2, ... , argn)
The control construct
LOOP() executes
each of the arguments
arg1, arg2,
...., argn in turn, repeatedly until either a
RETURN or
EXIT command is
encountered.
RETURN
expression
You will notice that we have called
PROG() and LOOP() control constructs as opposed to functions.
This is because they do not necessarily simplify all their arguments once, as functions
do, and then return a value.
IF(condition,then,else,undetermined
)
If the argument
condition is true then the argument
then is executed.
If the argument
condition
is false
then the argument
else
is
executed.
If Derive is unable to determine whether condition is
either true or false
then the argument undetermined
is executed.
Within a PROG() and or a LOOP(), the else
and undetermined arguments can be ommited. If it should be shown that
condition is false, then
the next argument of the PROG() or LOOP() is executed.
Try An Example
Or move on to Update Operators