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;