Académique Documents
Professionnel Documents
Culture Documents
Calling Sequence
proc (parameterSequence) :: returnType; local localSequence; global
globalSequence; option optionSequence; description descriptionSequence; uses
usesSequence; statementSequence end proc;
Parameters
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 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 (;).
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;
lc := proc s, u, t, v (1)
description "form a linear combination of the arguments";
s * u Ct * v
end proc
> lc p, x, KI, y
p x KI y (2)
> Describe lc
> lc
lc (3)
> eval lc
proc s, u, t, v (4)
description "form a linear combination of the arguments";
s * u Ct * v
end proc
> op 1, eval lc
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+a[i];
end do;
s:=s*x;
end proc;
addList := proc a::list, b::integer ::integer; (6)
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 Ca i end do; s := s * x
end proc
> sumList := addList 1, 2, 3, 4, 5 , 2
sumList := 30 (7)
Details
For details on defining, modifying, and handling parameters, see Procedure
Parameters.
See Also
_nresults, assertions, Function Calls, kernelopts, Last-Name Evaluation, Procedure
Options, ProcessOptions, procname, Reading and Saving Procedures, remember, return,
separator, Special Evaluation Rules, use