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