Re: Programming with options.
- To: mathgroup at smc.vnet.net
- Subject: [mg68079] Re: Programming with options.
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Fri, 21 Jul 2006 17:36:36 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <e9q99m$7v7$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
MaxArt wrote: > I want to create some new functions in Mathematica (4.0), and I want it to > work with options like Plot and other graphic functions. The idea behind > this is to modify the result and the behaviour of my functions when extra > arguments are given, and I thought that the use of options would be perfect. > For example, if I want to create a function Pow that gives the square of a > number, except when an option Exponent is given, so that Pow[a, Exponent -> > 3] yields a^3. First, you should not use Exponent since Exponent is already used by Mathematica: "Exponent[expr, form] gives the maximum power with which form appears in the expanded form of expr. Exponent[expr, form, h] applies h to the set of exponents with which form appears in expr." Second, a simple and efficient way of doing what you want can be found in using optional arguments and default values. For pow[a_, xponent_:2] := a^xponent pow[2] returns 4 pow[2, 3] returns 8 If you really insist in using options, you can find valuable posts in this newsgroup, search for "user defined option" for example. You could also have a look at the FilterOptions package [1]. HTH, Jean-Marc [1] http://documents.wolfram.com/mathematica/Add-onsLinks/StandardPackages/Utilities/FilterOptions.html