next up previous contents
Next: Evaluation and Simplification Up: Maple basics Previous: Functions

Procedures

Some functions require a complicated algorithm to compute. Maple has a complete programming language including looping statements, conditionals, and the other standard statements of any programming language. The core processing system of Maple is quite small; the vast majority of Maple commands are themselves programs written in Maple's programming language. Programs in Maple are called procedures, and are more general and complicated ways of describing functions.

The procedure begins with a proc command with the list of arguments in parentheses, in the form proc($arg_1,\; arg_2,\; \dots,arg_n$). Then there is a sequence of statements describing the procedure followed by end:, indicating the end. The last value computed is the value of the procedure; alternatively, the return value can be explicitly delivered by a RETURN statement. Here's an example.

> f:=proc(x,y,n)
> local z,j;
> z:= x;
> for j from 1 to n do
> z:= x*z+y;
> od;
> RETURN(z);
> end:
The local statement at the top specifies that j,z are variables that have meaning only while executing the procedure. Variables that have a global meaning in a Maple session should be identified with a global command. Inside the procedure, each Maple command should end with a semi-colon. The procedure initializes the local variable z to be the value of x. Then there is a loop of computations controlled by the for command. The block of statements in the for loop is enclosed within do and the terminating od;. To execute this command, we enter:
> f(2,3,5);

\begin{maplelatex} \begin{displaymath} 157 \end{displaymath}\end{maplelatex}
Is it right? That's what we call debugging. Check it out.


next up previous contents
Next: Evaluation and Simplification Up: Maple basics Previous: Functions
David J. Wright
2003年03月12日

AltStyle によって変換されたページ (->オリジナル) /