Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1995
*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 1995

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

Search the Archive

Re: Options in self-defined functions

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1668] Re: [mg1612] Options in self-defined functions
  • From: Allan Hayes <hay%haystack at christensen.cybernetics.net>
  • Date: Tue, 11 Jul 1995 04:47:16 -0400

Lancelot <Scott.A.Hill at williams.edu>
in [mg1612] Options in self-defined functions
wrote

> I would like to have a function I've written use options
> rather than parameters,... .Can I do it, and if so, how?

Scott,
Here is an example

Define the default values of the options

Options[f] = {delta->.03, epsilon->0.0005, trace->False};

The code allows for these values to be temporarily overridden

f[x_,y_, opts___?OptionQ] :=
	Module[{del,eps, tra},
		(*extract the values: the first value for each option 			
		name is used*)
		{del,eps,tra} =
			{delta,epsilon,trace}/.{opts}/.Options[f];
		(*use the values*)
		{x,y,del, eps,tra}
	]

Example
f[3,4, trace->True,epsilon -> .00001]
	
	{3, 4, 0.03, 0.00001, True}

You can redefine Options[f] like any other function value:

Options[f] = {delta->.5, epsilon->0.6, trace->True};

Or you can use the special function SetOptions to change some of  
the options.

SetOptions[f,epsilon -> .07]
{delta -> 0.03, epsilon -> 0.07, trace -> False}

SetOptions will also warn you about illegitimate option names:

SetOptions[f,hh->kk]

	SetOptions::optnf: hh is not a known option for f.
	SetOptions[f, hh -> kk]	
	
Allan Hayes
hay at haystack.demon.co.uk


  • Prev by Date: Re: I'm looking for an algorithm: Cartesian Product
  • Next by Date: Re: Does Mma solve INTEGER Problems
  • Previous by thread: Options in self-defined functions
  • Next by thread: Re: Options in self-defined functions