Implicit Local Variables
The Operands of a Procedure
proc (parameterSequence) :: returnType; local localSequence; global globalSequence; option optionSequence; description descriptionSequence; uses usesSequence; statementSequence end proc;
formal parameter declarations
(optional) assertion on the type of the returned value
(optional) names of local variables
(optional) names of global variables used in the procedure
(optional) names of procedure options
(optional) sequence of strings describing the procedure
(optional) names of modules or packages the procedure uses
statements comprising the body of the procedure
A procedure definition is a valid expression that can be assigned to a name. That name may then be used to refer to the procedure in order to invoke it in a function call.
The parenthesized parameterSequence, which may be empty, specifies the names and optionally the types and/or default values of the procedure's parameters. In its simplest form, the parameterSequence is just a comma-separated list of symbols by which arguments may be referred to within the procedure.
More complex parameter declarations are possible in the parameterSequence, including the ability to declare the type that each argument must have, default values for each parameter, evaluation rules for arguments, dependencies between parameters, and a limit on the number of arguments that may be passed. See Procedure Parameters for more details on these capabilities.
The closing parenthesis of the parameterSequence may optionally be followed by ::, a returnType, and a ;. This is not a type declaration, but rather an assertion. If kernelopts(assertlevel) is set to 2, the type of the returned value is checked as the procedure returns. If the type violates the assertion, then an exception is raised.
Each of the clauses local localSequence;, global globalSequence;, option optionSequence;, description descriptionSequence;, and uses usesSequence; is optional. If present, they specify respectively, the local variables reserved for use by the procedure, the global variables used or modified by the procedure, any procedure options, a description of the procedure, and any modules or packages used by the procedure. These clauses may appear in any order.
Local variables that appear in the local localSequence; clause may optionally be followed by :: and a type. As in the case of the optional returnType, this is not a type declaration, but rather an assertion. If kernelopts(assertlevel) is set to 2, any assignment to a variable with a type assertion is checked before the assignment is carried out. If the assignment violates the assertion, then an exception is raised.
A global variable declaration in the global globalSequence clause cannot have a type specification.
Several options that affect a procedure's behavior can be specified in the option optionSequence; clause. These are described in detail on their own page.
The description descriptionSequence; clause specifies one or more lines of description about the procedure. When the procedure is printed, this description information is also printed. Even library procedures, whose body is generally elided when printing, have their description (if any) printed. The descriptionSequence is also used when information about the procedure is printed by the Describe command.
The optional uses usesSequence; clause is equivalent to wrapping the statementSequence with a use statement. In other words,
proc ... uses LinearAlgebra; ... end proc
is equivalent to:
proc ... use LinearAlgebra in ... end use; end proc
The statementSequence consists of one or more Maple language statements, separated by semicolons (;), implementing the algorithm of the procedure.
A procedure assigned to a name, f, is invoked by using f(argumentSequence). See Argument Processing for an explanation of argument passing.
The value of a procedure invocation is the value of the last statement executed, or the value specified in a return statement.
In both 1-D and 2-D math notation, statements entered between proc and end proc must be terminated with a colon (:) or semicolon (;).
For any variable used within a procedure without being explicitly mentioned in a local localSequence; or global globalSequence; the following rules are used to determine whether it is local or global:
The variable is searched for amongst the locals and globals (explicit or implicit) in surrounding procedures, starting with the innermost. If the name is encountered as a parameter, local variable, or global variable of such a surrounding procedure, that is what it refers to.
Otherwise, any variable to which an assignment is made, or which appears as the controlling variable in a `for` loop, is automatically made local.
Any remaining variables are considered to be global.
Note: Any name beginning with _Env is considered to be an environment variable, and is not subject to the rules above.
A Maple procedure is a valid expression like any other (e.g. integers, sums, inequalities, lists, etc.). As such, it has sub-parts that can be extracted using the op function. A procedure has eight such operands:
op 1 is the parameterSequence,
op 2 is the localSequence,
op 3 is the optionSequence,
op 4 is the remember table,
op 5 is the descriptionSequence,
op 6 is the globalSequence,
op 7 is the lexical table (see note below), and
op 8 is the returnType (if present).
Any of these operands will be NULL if the corresponding sub-part of the procedure is not present.
Note: The lexical table is an internal structure used to record the correspondence between undeclared variables and locals, globals, or parameters of surrounding procedures. It does not correspond to any part of the procedure as written.
Procedures have special evaluation rules (like tables) so that if the name f has been assigned a procedure then:
f evaluates to just the name f,
eval(f) yields the actual procedure, and
op(eval(f)) yields the sequence of eight operands mentioned above (any or all of which may be NULL).
Within a procedure, during the execution of its statementSequence, local variables have single level evaluation. This means that using a variable in an expression will yield the current value of that variable, rather than first evaluating that value. This is in contrast to how variables are evaluated outside of a procedure, but is similar to how variables work in other programming languages.
Remember tables (option remember) should not be used for procedures that are intended to accept mutable objects (e.g., rtables or tables) as input, because Maple does not detect that such an object has changed when retrieving values from remember tables.
lc := proc( s, u, t, v )
description "form a linear combination of the arguments";
s * u + t * v
lc ≔ procs,u,t,vdescriptionform a linear combination of the arguments;s*u+t*vend proc
# form a linear combination of the arguments
lc( s, u, t, v )
procs,u,t,vdescriptionform a linear combination of the arguments;s*u+t*vend proc
addList := proc(a::list,b::integer)::integer;
description "add a list of numbers and multiply by a constant";
for i in a do
addList ≔ proca::list,b::integer::integer;localx,i,s;descriptionadd a list of numbers and multiply by a constant;x ≔ b;s ≔ 0;foriinados ≔ i+send do;s ≔ s*xend proc
sumList ≔ addList⁡1,3,5,7,2
For details on defining, modifying, and handling parameters, see Procedure Parameters.
Reading and Saving Procedures
Special Evaluation Rules
Download Help Document