Procedures The syntax of the procedure statement is: proc (name-sequence) local name-sequence; options name-sequence; statement-sequence; end; The proc (name-sequence) is the procedure header, and the name-sequence is a formal parameter list. These are the internal variable names for values that you would pass during calls to the procedure. The local name-sequence is an optional clause listing the local variables, and options name-sequence is an optional clause of settings that influence the procedure's behaviour at runtime. Maple documentation variously refers to certain routines as being "functions" or "procedures". Syntactically, they are all the same kind of object, and are all implemented using proc. The only difference between functions and procedures is in side-effects. Functions use the values passed as parameters, but otherwise leave those arguments alone. Procedures may treat some of those arguments as variable names, and modify the value that the variable name refers to. This is the same distinction as you find in other programming languages that allow both "pass by value" and "pass by reference" parameters. Notice that there is no specification for the name of the procedure. Since procedures are objects that can be assigned to variables, the name of the variable you assign it to is used as the name of procedure. The value of the procedure is the value of the last expression evaluated (though see the section on the RETURN statement, later in this document). Example: > # Out of curiosity, see if "x" and "y" are mathematical variables: > x, y; x, y > f := proc(x) > x^2 + 1; > y := x; > end; f := proc(x) x^2+1 end > f(2); 5 > x, y; x, 2 In this example, the global variable "x" is not affected by the formal parameter "x", which is local to function "f". The global variable "y" was affected, since it is not a parameter and was not declared as a local variable. Local Variables When you do not specify local variables in a procedure then all variable references are assumed to be to global variables, except for references to the formal parameters. Specified local variables are hidden from "the outside world", and themselves hide global variables of the same name from "the inside world" of the procedure. Local variables also have one other very important distinction: they are not subject to the default full evaluation. This is because local variables commonly do not require full evaluation, so performance is improved by this exception to the general evaluation scheme. Example: > # Set value of global "a": > a := 0; a := 0 > g := proc(x) > local a, b; > a := b^2 + x; > print(a); > b := 2; > print(`a:`, a, ` eval a:`, eval(a), ` a:`, a); > end: > g(2); 2 b + 2 2 2 a:, b + 2, eval a:, 6, a:, b + 2 > # So full evaluation doesn't happen to local variable "a", even > # if evaluation is called. You'd have to assign the result of > # "eval(a)" to "a" to update its value. > a; 0 > # Just to check, the global variable "a" is not affected by > # the local variable "a" in function "g". Options Options are used to control run-time behaviour of a procedure. The options defined for Maple V are builtin, remember, system, operator, and trace. remember is the one of greatest use to most users. When you use option remember, the values that correspond to particular parameter lists are stored for future use. This way they can be retrieved instead of recalculated, a feature particularly useful for recursive function implementations. You can force remember for particular argument values, whether or not option remember has been specified. You simply perform an assignment to the function with the appropriate parameter value. For example: > h(2) := 3; h(2) := 3 > h(2); 3 This is why it is necessary to use proc to define functions. It is tempting to try syntactic definitions like m(x) := x^2, but what you are really doing is stating something like "the value of function m at the string `x` has the value `x`^2", assuming "x" was a mathematical variable when you entered in the assignment statement. When you try to plot this pseudo-function, you will find that some forms of plotting (like parameterized 3-D plotting) do not work. RETURN The syntax of the RETURN statement is simply: RETURN(value); The procedure terminates processing and returns the value specified. If an explicit RETURN(...) does not terminate the procedure, then the value of the procedure is result of the last expression evaluated before the procedure terminated normally.