Named arguments in Mathematica.

Posted by dreeves on Stack Overflow See other posts from Stack Overflow or by dreeves
Published on 2009-10-23T20:54:21Z Indexed on 2010/06/17 14:03 UTC
Read the original article Hit count: 295

What's the best/canonical way to define a function with optional named arguments? To make it concrete, let's create a function foo with named arguments a, b, and c, which default to 1, 2, and 3, respectively. For comparison, here's a version of foo with positional arguments:

foo[a_:1, b_:2, c_:3] := bar[a,b,c]

Here is sample input and output for the named-arguments version of foo:

foo[]                  --> bar[1,2,3]
foo[b->7]              --> bar[1,7,3]
foo[a->6, b->7, c->8]  --> bar[6,7,8]

It should of course also be easy to have positional arguments before the named arguments.

© Stack Overflow or respective owner

Related posts about functions

Related posts about mathematica