
Calling Sequence


proc (parameterSequence) :: returnType; local localSequence; global globalSequence; option optionSequence; description descriptionSequence; uses usesSequence; statementSequence end proc;


Parameters


parameterSequence



formal parameter declarations

returnType



(optional) assertion on the type of the returned value

localSequence



(optional) names of local variables

globalSequence



(optional) names of global variables used in the procedure

optionSequence



(optional) names of procedure options

descriptionSequence



(optional) sequence of strings describing the procedure

usesSequence



(optional) names of modules or packages the procedure uses

statementSequence



statements comprising the body of the procedure





Description


•

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 commaseparated list of symbols by which arguments may be referred to within the procedure.

•

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


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 1D and 2D math notation, statements entered between proc and end proc must be terminated with a colon (:) or semicolon (;).



Implicit Local Variables


•

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.



The Operands of a Procedure


•

A Maple procedure is a valid expression like any other (e.g. integers, sums, inequalities, lists, etc.). As such, it has subparts 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 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 subpart 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.



Evaluation Rules


•

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.



Notes


•

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.



Examples


>

lc := proc( s, u, t, v )
description "form a linear combination of the arguments";
s * u + t * v
end proc;

${\mathrm{lc}}{:=}{\mathbf{proc}}\left({s}{\,}{u}{\,}{t}{\,}{v}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{description}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{''form\; a\; linear\; combination\; of\; the\; arguments''}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{s}{\*}{u}{\+}{t}{\*}{v}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; proc}}$
 (1) 
>

$\mathrm{lc}\left(\mathrm{\pi}\,x\,\mathrm{I}\,y\right)$

${\mathrm{\pi}}{}{x}{}{\mathrm{I}}{}{y}$
 (2) 
>

$\mathrm{Describe}\left(\mathrm{lc}\right)$

# form a linear combination of the arguments
lc( s, u, t, v )
 
>

$\mathrm{eval}\left(\mathrm{lc}\right)$

${\mathbf{proc}}\left({s}{\,}{u}{\,}{t}{\,}{v}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{description}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{''form\; a\; linear\; combination\; of\; the\; arguments''}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{s}{\*}{u}{\+}{t}{\*}{v}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; proc}}$
 (4) 
>

$\mathrm{op}\left(1\,\mathrm{eval}\left(\mathrm{lc}\right)\right)$

${s}{,}{u}{,}{t}{,}{v}$
 (5) 
>

addList := proc(a::list,b::integer)::integer;
local x,i,s;
description "add a list of numbers and multiply by a constant";
x:=b;
s:=0;
for i in a do
s:=s+i;
end do;
s:=s*x;
end proc;

${\mathrm{addList}}{:=}{\mathbf{proc}}\left({a}{::}{\mathrm{list}}{\,}{b}{::}{\mathrm{integer}}\right){::}{\mathrm{integer}}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{x}{\,}{i}{\,}{s}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{description}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{''add\; a\; list\; of\; numbers\; and\; multiply\; by\; a\; constant''}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{x}{:=}{b}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{s}{:=}{0}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{i}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{in}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{a}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{s}{:=}{i}{\+}{s}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; do}}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{s}{:=}{s}{\*}{x}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; proc}}$
 (6) 
>

$\mathrm{sumList}\u2254\mathrm{addList}\left(\left[1\,3\,5\,7\right]\,2\right)$

${\mathrm{sumList}}{\u2254}{32}$
 (7) 


