Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Options in user-defined functions...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg60375] Re: Options in user-defined functions...
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Wed, 14 Sep 2005 03:27:51 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 9/13/05 at 6:07 AM, danwolf80_no_spam_ at libero.it (Daniele Lupo)
wrote:

>I've a little problem with a function. I've defined it in this way:

>fun[x_,op1_:1,op2_:2,op3_:3]:=
>Module[{....},f1[x,op1],f2[x,op2],f3[x,op3]]

>So, when I use it, ops have their default values.

>It works, but I'd like to use option; this why, if I want to change
>only op3, I must to write every argument when I call the function,
>while I'd like to write something like this:

>fun[x,op3->4]

>how can I do it?

Basically, you need to rewrite your function along the lines of:

SetOptions[fun, op1->1, op2->2, op3->3];

fun[x_, opts___]:=
Module[{....},
  f1[x,op1]/.{opts,Options[fun]};
  f2[x,op2]/.{opts,Options[fun]};
  f3[x,op3]/.{opts,Options[fun]}]

A couple of additional thoughts. The package Utilities`FilterOPtions` is quite handy when creating functions with options. It allows you to call other functions which have options, ensuring each function gets only those options applicable to it.

Roman Maeder presents a very clear discussion of options versus default values in chapter 3 of his book Programming in Mathematica.
--
To reply via email subtract one hundred and four


  • Prev by Date: Batch
  • Next by Date: Re: Generate a palette with a FrontEnd Token
  • Previous by thread: Re: Options in user-defined functions...
  • Next by thread: Re: Options in user-defined functions...