Next: Tracing Procedure Execution: Up: Maple Procedures Previous: Maple Procedures

Parameters, Local Variables, RETURN, ERROR

A Maple procedure has the following syntax

proc ( nameseq )

[ local nameseq ; ]

[ options nameseq ; ]

statseq

end

where nameseq is a sequence of symbols separated by commas, and statseq is a sequence of statements separated by semicolons. Here is a simple procedure which, given , computes .


proc(x,y) x^2 + y^2 end

This procedure has two parameters and . It has no local variables, no options, and only one statement. The value returned by the procedure is . In general the value returned by a procedure is the last value computed unless there is an explicit return statement. An example of a procedure with an explicit return statement is the following MEMBER procedure. MEMBER(x,L) returns true if is in the list , false otherwise.


MEMBER := proc(x,a) local v;
    for v in L do if v = x then RETURN(true) fi od;
    false
end;

The MEMBER procedure has a local variable so that it does not interfere with the global user variable . Variables that appear in a procedure which are neither parameters nor local variables are global variables.

The ERROR function can be used to generate an error message from within a procedure. For example, the MEMBER routine should check that the argument really is a list and issue an appropriate error message otherwise.


MEMBER := proc(x,L) local v;
    if not type(L,list) then ERROR(`2nd argument must be a list`) fi;
    for v in L do if v = x then RETURN(true) fi od;
    false
end;

A more succinct notation for simple argument checking if provided in Maple V Release 2. One writes


MEMBER := proc(x,L:list) local v;
    for v in L do if v = x then RETURN(true) fi od;
    false
end;

Note, this is not a type declaration like in Pascal or C. All it is is a shortcut for the run-time type checking that we coded explicitly in the previous version of MEMBER. It does however produce a more useful error message which will greatly help you in tracking down errors in your programs. For example, =-1.00.5plus##1`##1=12=^^M=12 > MEMBER([1,x,x^2],x); Error, MEMBER expects its 2nd argument, L, to be of type list, but received x

plusplus -100 plus



Next: Tracing Procedure Execution: Up: Maple Procedures Previous: Maple Procedures


Klaus Steinberger
Mi Apr 13 12:51:51 MDT 1994