If a user defined function is declared with N arguments, then the function is callable with N arguments only.
[0] def factor(A) { return fctr(A); } [1] factor(x^5-1,3); evalf : argument mismatch in factor() return to toplevel
A function with indefinite number of arguments can be realized by using a list or an array as its argument. Another method is available as follows:
% cat factor def factor(F) { Mod = getopt(mod); ModType = type(Mod); if ( ModType == 1 ) /* 'mod' is not specified. */ return fctr(F); else if ( ModType == 0 ) /* 'mod' is a number */ return modfctr(F,Mod); }
[0] load("factor")$ [1] factor(x^5-1); [[1,1],[x-1,1],[x^4+x^3+x^2+x+1,1]] [2] factor(x^5-1|mod=11); [[1,1],[x+6,1],[x+2,1],[x+10,1],[x+7,1],[x+8,1]]
In the second call of factor()
, |mod=11
is placed
after the argument x^5-1
, which appears in the declaration of
factor()
. This means that the value 11
is assigned to
the keyword mod
when the function is executed. The value
can be retrieved by getopt(mod)
. We call such machinery
option. If the option for mod is not specified,
getopt(mod)
returns an object whose type is -1. By this
feature, one can describe the behaviour of the function when
the option is not specified by if statements.
After `|' one can append any number of options seperated by `,'.
[100] xxx(1,2,x^2-1,[1,2,3]|proc=1,index=5);
Go to the first, previous, next, last section, table of contents.