Contents Previous Next Index
|
6 Procedures
|
|
A Maple procedure is a sequence of parameter declarations, variable declarations, and statements that encapsulates a computation. Once defined, a procedure can be used to perform the same computation repeatedly for different argument values, from different places in a program, or both. A procedure in Maple corresponds to a function in languages such as C or Java, a procedure or function in Pascal, or a subroutine in FORTRAN and modern versions of BASIC.
Chapter 1 gave a brief introduction to procedures. This chapter describes the syntax and semantics of procedures in detail, and discusses how to best make use of procedures in your programs.
|
6.1 Terminology
|
|
Several terms are used frequently when discussing procedures in Maple and other programming languages. Some of these terms are sometimes used interchangeably, but the distinctions between them 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 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 or Formal 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.
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 for completeness; it is not further used in this chapter. Instead we will refer to the value of the parameter.
|
|
6.2 Defining and Executing Procedures
|
|
A Maple procedure definition has the following general syntax:
proc( parameterDeclarations ) :: returnType;
|
description shortDescription;
|
option optionSequence;
|
local localVariableDeclarations;
|
global globalVariableDeclarations;
|
statementSequence
|
end proc
|
|
|
A procedure definition is considered to be an expression in Maple, the evaluation of which produces the procedure itself. The resulting procedure is usually assigned to a name, but it can also be used in other ways such as passing it as an argument to another procedure, or invoking it immediately.
The following is a simple procedure definition. It contains two formal parameters, x and y, and one statement in the procedure body. There is no description, there are no options, and the procedure does not make use of any local or global variables. In order to be able to use the procedure later, we'll assign it to a name:
>
|
SumOfSquares := proc( x, y )
x^2 + y^2
end proc;
|
| (1) |
This procedure computes the sum of the squares of its two arguments. The procedure can be called with any two arguments and Maple will attempt to compute the sum of their squares. Like any computation in Maple, the result can be symbolic. If you want to restrict the types of arguments that are permitted, it is possible to specify the type for each argument in the parameter declarations, as described in the next section.
You can invoke (or execute) a procedure by using it in a function call:
procedureName( argumentSequence )
|
|
|
The procedureName is usually the name that the procedure was assigned to, although it can also be an actual procedure definition, or another expression that evaluates to a procedure.
The argumentSequence is a sequence of expressions that will be evaluated, and then substituted for the corresponding parameters before the execution of the statements comprising the body of the procedure. Note that the arguments are evaluated only once before the execution of the procedure begins. They are not evaluated again during execution of the procedure.
The value returned by the procedure is the result of the last statement executed within the procedure. In the following function call, Maple executes the statements in the body of the procedure SumOfSquares, replacing the formal parameters x and y with the arguments a and 3. The result of the last (and in this case, only) statement in the procedure is the returned value:
| (2) |
For more information about return values, see Returning Values from a Procedure.
|
|
6.3 Parameter Declarations
|
|
In the procedure definition, parameterDeclarations is a sequence of parameter declarations. Procedure parameter declarations can range from very simple to very sophisticated. In its simplest form, a parameter declaration is just the parameter's name. When you call the procedure, you can pass any value as an argument for such a parameter, and if you pass no value at all, the parameter will have no value.
You can extend a parameter declaration by adding a type specification and/or a default value. A type specification ensures that, when the procedure is called, the value of the parameter within the procedure will be of the indicated type, and a default value ensures that a parameter will always have a value even if no corresponding argument was passed.
Maple procedures can also have keyword parameters. When invoking a procedure, the corresponding arguments are of the form keyword=value, and can appear anywhere in the argument sequence.
When you call a procedure, the arguments are evaluated and then bound to the parameters. In the simplest case, there is a one-to-one correspondence between arguments and parameters; the first argument is bound to the first parameter, the second argument to the second parameter, and so on. The presence of default values and keyword parameters can change this correspondence, as described in this section.
|
Required Positional Parameters
|
|
A required positional parameter is called required because a corresponding argument must have been passed in the function call that invoked the procedure if the parameter is used during the execution of the procedure. It is called positional because the argument's position within argumentSequence must correspond to the position of the parameter in parameterDeclarations.
The syntax to declare a required positional parameter is:
parameterName :: parameterType
|
|
|
The parameterName must be a valid symbol, and is used to refer to the parameter within the procedure body. The :: parameterType is optional. If it is present and the corresponding argument does not match the specified type, an exception is raised.
In this example, the procedure Adder is defined with two parameters, a and b. The procedure returns the sum of its two arguments. For the parameter a, Adder expects an argument of type integer.
>
|
Adder := proc( a::integer, b ) a+b end proc:
|
| (3) |
The next call to Adder raises an exception because the second argument is missing.
This call raises an exception because the supplied first argument does not match the parameter's specified type.
| | | |