Procedural Programming
Using the PROG and LOOP program control constructs in user-defined functions makes it possible to write multi-line, procedural programs in Derive. Both these control constructs take any number of program statements (e.g. assignment statements and if-then-else statements). The statements of a PROG construct are sequentially evaluated until an EXIT or RETURN statement is encountered or until its last statement is evaluated, which is returned as the value of the PROG construct. The statements of a LOOP construct are sequentially evaluated repeatedly until an EXIT or RETURN statement is encountered, which terminates the loop and the last statement evaluated is returned as the value of the LOOP construct.
Temporary assignments to local variablesLocal_variables of the function being defined are commonly made within the scope of the PROG and LOOP program control constructs. Upon exit from the user-defined function, these variables are restored to their original values. However, if assignments are made to non-local variables, these variables are not restored to their original values upon exit from the function. Also, if a vector or matrix is assigned to different local variables, assignments to the elements of the vector or matrix affect the value of all the local variables.
If a RETURN statement is encountered at any point in the evaluation of a user-defined function, evaluation terminates and the value of RETURN's argument is returned as the value of the function. If an EXIT statement is encountered at any point in the evaluation of a PROG or LOOP control construct, evaluation of the construct terminates and evaluation continues with the next statement in the user-defined function.
The following definition of Rev illustrates the use of the PROG and LOOP control constructs to reverse the elements of a vector. (Note that the built-in function REVERSE can also be used to reverse the elements of a vector.) Rev uses the built-in functions FIRST, REST, and ADJOIN (see Vector Manipulation FunctionsVector_Manipulation_Functions) to perform operations on vectors analogous to the way lists are processed in the LISP programming language.
Rev(v,w) := PROG(w:=[], LOOP(IF(v=[], RETURN w), w:=ADJOIN(FIRST(v),w), v:=REST(v)))
After entry, the definition is displayed in the more readable, multi-line indented format as
Rev(v, w) :=
Prog
w := []
Loop
If v = []
RETURN w
w := ADJOIN(FIRST(v), w)
v := REST(v)
Then, for example,
Rev([a, b, c])
simplifies to [c, b, a].
The default initial value of a user-defined function’s local variablesLocal_variables can be specified by including assignment statements in the function's formal argument list. If a function is called with fewer actual arguments than it has local variables, the extra local variables are given their assigned values. For example, if SUMSQ is defined as
SUMSQ(x, y := 0, z := 0) := x^2 + y^2 + z^2
then the vector
[SUMSQ(5), SUMSQ(5, 3), SUMSQ(5, 3, 2)]
simplifies to [25, 34, 38]. Whereas, if SUMSQ is defined as
SUMSQ(x, y, z) := x^2 + y^2 + z^2
then the above vector simplifies to
⎡ 2 2 2 ⎤
⎣y + z + 25, z + 34, 38⎦
Note that the default initial value can also be specified for the local variables of arbitrary user-defined functions. For example, if FOO is defined as
FOO(x, y := 3, z) :=
then
FOO(5)
simplifies to FOO(5,3,z).
Specifying the default initial value of one or more of a function’s local variables often makes it possible to avoid using a PROG construct in the function’s definition. For example, initializing the accumulator variable w to the empty vector shortens the definition of Rev to
Rev(v,w:=[]) := LOOP(IF(v=[], RETURN w), w:=ADJOIN(FIRST(v),w), v:=REST(v))
Alternatively, Rev can be even more elegantly defined using recursion as
Rev(v,w:=[]) := IF(v=[], w, Rev(REST(v),ADJOIN(FIRST(v),w)))
The evaluation of expressions in function definitions can be suppressed using the quote operator. The single quote mark is used for the quote operator. For example,
'(2+3)
simplifies to 2+3 instead of 5. The quote operator is useful for quoting expressions in calls on functions that you do not want evaluated before being passed. For example,
TERMS('(x + x + x))
simplifies to the [x, x, x]; whereas
TERMS((x + x + x))
simplifies to [3·x].
User functions can be defined so they will accept any number of arguments. If the single formal argument of a user-defined function is not enclosed in parentheses, then all the arguments in calls on the function are passed to the function as a vector. This allows user-defined function to accept any number of arguments. For example, after making the definition
FOO v := SUM(v)
then
FOO(2, 3, 5)
simplifies to 10 and
FOO(2, 3, 5, 7, 11)
simplifies to 28.
Other Programming in DERIVEProgramming
Created with the Personal Edition of HelpNDoc: Create cross-platform Qt Help files