Next: LOOPS Up: A Short Maple Primer Previous: HELP FACILITIES

USER-DEFINED FUNCTIONS

There are several possibilities for writing your own functions in Maple. The simplest is probably the arrow-construct. An example:


> f:=x -> x^2-a;
                                      2
                           f := x -> x  - a
> g:=(x,y) -> x*y-1;

                         g := (x,y) -> x y - 1

> g(f(1),2);
                               - 2 a + 1

If the function is more complicated and local variables are needed the function proc should be used. In the example below the function myfcn takes one argument (a number) and returns another number using the local variables t and u during the computation.


myfcn:=proc(x)
   local t,u:
   if x>=0 then t:=evalf(sqrt(x),3) else t:=x^2 fi:
   if t>2 then u:=t+x else u:=t-x fi:
   max(t,u)
end:

The value returned from the procedure is the one of the last expression - the one before end. If the number of in-arguments is not predetermined, use args and nargs.

The alias facility is quite useful. An example:


alias(tp=transpose,SE=simplify@expand,choose=binomial):


bondaren@thsun1.jinr.ru