Macaulay2 » Documentation
Packages » Macaulay2Doc » The Macaulay2 language » making new functions with optional arguments
next | previous | forward | backward | up | index | toc

# making new functions with optional arguments

Let's consider an example where we wish to construct a linear function of x called f, with the slope and y-intercept of the graph being optional arguments of f. We use the >> operator to attach the default values to our function, coded in a special way.

    i1 : opts = {Slope => 1, Intercept => 1}

    o1 = {Slope => 1, Intercept => 1}

    o1 : List

    i2 : f = opts >> o -> x -> x * o.Slope + o.Intercept

    o2 = f

    o2 : FunctionClosure

    i3 : f 5

    o3 = 6

    i4 : f(5, Slope => 100)

    o4 = 501

    i5 : f(5, Slope => 100, Intercept => 1000)

    o5 = 1500

In the example the function body is the code x * opts.Slope + opts.Intercept. When it is evaluated, a hash table is assigned to opts; its keys are the names of the optional arguments, and the values are the corresponding current values, obtained either from the default values specified in the definition of f, or from the options specified at the time f is called.

In the example above, the inner function has just one argument, x, but handling multiple arguments is just as easy. Here is an example with two arguments.

    i6 : f = {a => 1000} >> o -> (x,y) -> x * o.a + y;

    i7 : f(3,7)

    o7 = 3007

    i8 : f(5,11,a=>10^20)

    o8 = 500000000000000000011

### Menu

- symbols used as the name or value of an optional argument

The source of this document is in /build/macaulay2-88fgJW/macaulay2-1.25.11+ds/M2/Macaulay2/packages/Macaulay2Doc/ov_language.m2:539:0.