Next: Programming in Maple Up: Maple Procedures Previous: Optional Arguments and

Returning Results Through Parameters

Many functions in Maple return more than one value. Of course, it is always possible to return more than one value in a sequence or list. However, it is also possible to return values through parameters like in other programming languages, and often, this is more convenient. For example, consider the divide function in Maple which does polynomial long division. The call divide(a,b) returns true if and only if the polynomial divides the polynomial with no remainder, e.g.

=-1.00.5plus##1`##1=12=^^M=12 > divide(x^3-1,x-1);

true

plusplus -100 plus But usually, if divides , one wants to do something with the quotient . In Maple, this can be done by giving the divide function a third parameter which is a name which will be assigned the quotient if divides , e.g.

=-1.00.5plus##1`##1=12=^^M=12 > if divide(x^3-1,x-1,'q') then print(q) fi;

2 x + x + 1

plusplus -100 plus Notice the use of quotes here to pass to the divide function the name and not the value of . Let us consider another example and study how to write a program which assigns a value to an optional parameter. Consider our MEMBER function which tested to see if a value appears in a list . Let us modify our function such that MEMBER(x,L,'p') still returns whether appears in the list , and in addition, assigns the name the position of the first appearance of in .


MEMBER := proc(x,L:list,p:name) local i;
    for i to nops(L) do
        if x = L[i] then
            if nargs = 3 then p := i fi;
            RETURN(true)
        fi
    od;
    false
end;

Here is an example

=-1.00.5plus##1`##1=12=^^M=12 > MEMBER(4,[1,3,5],'position');

false

> position;

position

> MEMBER(3,[1,3,5],'position');

true

> position;

2

plusplus -100 plus We see that the effect of the assignment to the formal parameter inside the MEMBER procedure is that the actual parameter position is assigned.



Next: Programming in Maple Up: Maple Procedures Previous: Optional Arguments and


Klaus Steinberger
Mi Apr 13 12:51:51 MDT 1994