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.
> divide(x^3-1,x-1); true
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.
> if divide(x^3-1,x-1,'q') then print(q) fi; 2 x + x + 1
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,p) 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
> MEMBER(4,[1,3,5],'position'); false > position; position > MEMBER(3,[1,3,5],'position'); true > position; 2
We see that the effect of the assignment to the formal parameter
inside the MEMBER procedure is that the actual parameter position
is assigned.