Go to the first, previous, next, last section, table of contents.


option

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);

Optinal arguments may be given as a list with the key word option_list as option_list=[["key1",value1],["key2",value2],...]. It is equivalent to pass the optional arguments as key1=value1,key2=value2,....

[101] dp_gr_main([x^2+y^2-1,x*y-1]|option_list=[["v",[x,y]],["order",[[x,5,y,1]]]]); 

Since getopt() returns an option list, the optional argument option_list=... is useful when we call functions with optional arguments from a function with optional arguments to pass the all optional parameters.

% cat foo.rr
def foo(F)
{
    OPTS=getopt();
    return factor(F|option_list=OPTS);
}
[3] load("foo.rr")$
[4] foo(x^5-1|mod=11);
[[1,1],[x+6,1],[x+2,1],[x+10,1],[x+7,1],[x+8,1]]


Go to the first, previous, next, last section, table of contents.