Introduction to Procedure Parameters
Description
Terminology
Procedure Definition Syntax
Parameters are used to refer to values that are passed to a Maple procedure. Such procedures are invoked by function calls, to which arguments are provided.
There are several terms that are used frequently when discussing the calling of procedures in Maple and other programming languages. Often, these terms are used interchangeably, but their distinctions are important.
Procedure - In Maple, a procedure is an object that can be invoked by a function call, be passed arguments, perform some operations, and optionally return a result. A procedure definition begins with the keyword proc, and ends with end proc.
Function Call - A function call, of the form name(arguments), evaluates the arguments and then invokes a procedure if name has a value that is a procedure. The value of the function call is then the value returned by the procedure. If name has no value, then the value of the function call is just name⁡evaluatedArguments.
Argument - An argument is one of one or more values explicitly included in a function call. Note that a default value is not an argument.
Parameter - A parameter is a name that is declared in a procedure definition to receive the value of an argument. The parameter name is used to refer to that value within the body of the procedure. A parameter is also known as a formal parameter.
Actual Parameter - An actual parameter is neither an argument nor a (formal) parameter. The term refers to the value that a formal parameter takes during the execution of a procedure. This value can come from an argument or a default value. The term is defined here only for completeness; it is not further used in these pages. Instead we will refer to "the value of the parameter".
In the following example, sgn is assigned a procedure that returns -1, 1, or 0 depending on whether its parameter x represents a value that is less than, greater than, or equal to zero, respectively.
After the procedure has been defined and assigned to sgn, a function call is made, passing to the procedure the argument 3.5, which becomes the value of the parameter x during the execution of the procedure.
sgn := proc( x ) if x < 0 then -1 elif x > 0 then 1 else 0 end if end proc:
sgn(3.5);
1
In general, a procedure in Maple is defined using the following syntax:
procedureName := proc( parameterDeclarations ) :: returnType; statementSequence; end proc;
The procedureName := part is not part of the procedure definition. Rather, it serves to assign the procedure to a symbol by which it may be referred to (see assignment).
The return type declaration, ::returnType, is optional, and may be omitted. If present, it acts as an assertion. If assertions are enabled (see kernelopts), and the procedure attempts to return a value that does not match the declared return type, an assertion failure exception is raised.
The statementSequence is the body of the procedure. It contains the Maple statements (e.g. assignments, conditional statements, loops, etc.) that implement what the procedure does.
The parameterDeclarations define symbols that can receive values when the procedure is invoked. How such parameters are declared, used, and assigned values are the subject of these help pages:
1. Parameter Declarations
2. Parameter Modifiers
3. Using Parameters in Procedures
4. Argument Processing
See Also
Procedures
Download Help Document