DefaultValues

DefaultValues[f]

gives a list of transformation rules corresponding to default values of f.

DefaultValues["symbol"]

gives a list of transformation rules corresponding to all default values defined for the symbol named "symbol" if it exists.

Details and Options

  • DefaultValues[f] gives rules corresponding to default positional arguments as well as the default options of f.
  • Transformation rules for Default[f] define the default for all positions. »
  • Transformation rules for Default[f,i] define the default for optional arguments in the i^(th) position of f. »
  • Transformation rules for Default[f,i,n] define the default for optional arguments in the i^(th) position of f when f is called with n arguments. »
  • Transformation rules for Options[f] define the default options of f. »
  • You can specify the defaults for f by making an assignment of the form DefaultValues[f]=list.
  • The list returned by DefaultValues has elements of the form HoldPattern[lhs]:>rhs.
  • DefaultValues[f] only affects pattern matching and does not create any evaluation rules for f. »
  • Default values are not cleared by Clear. Use ClearAll to clear them. »

Examples

open allclose all

Basic Examples  (1)

Define a default argument for a function f:

These are the defaults associated with f:

Scope  (3)

DefaultValues returns rules corresponding to default arguments for a function:

Create several values:

Obtain the default values of symbols whose names start with f:

DefaultValues can be used to set the values directly:

Applications  (2)

The resulting rules are in the order given:

Now reorder the definitions:

Copy a symbol's defaults to another symbol:

Properties & Relations  (10)

Assigning a value to Default[f] will define default arguments for all positions:

Assigning values to Default[f,i] will define default arguments for the i^(th) position:

Assigning values to Default[f,i,n] will define default arguments for the i^(th) position when f is called with n arguments:

Assigning values to Options[f] will define default options:

DefaultValues[f] only affects pattern matching for f:

It does not on its own create any transformation rules for f[]:

Defaults can be defined by immediate or delayed assignments:

HoldPattern is used to protect the rules from their own definitions:

Without the HoldPattern, the left-hand side would have evaluated:

DefaultValues["sym"] will issue a message if the specified symbol does not exist:

If the symbol exists but has no definitions, an empty list is returned:

Definition and Information display defaults but do not return them as values:

DefaultValues returns a value that can be used in a program:

Use ClearAll to clear default values:

Clear does not clear them:

Wolfram Research (1991), DefaultValues, Wolfram Language function, https://reference.wolfram.com/language/ref/DefaultValues.html (updated 2022).

Text

Wolfram Research (1991), DefaultValues, Wolfram Language function, https://reference.wolfram.com/language/ref/DefaultValues.html (updated 2022).

CMS

Wolfram Language. 1991. "DefaultValues." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/DefaultValues.html.

APA

Wolfram Language. (1991). DefaultValues. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DefaultValues.html

BibTeX

@misc{reference.wolfram_2024_defaultvalues, author="Wolfram Research", title="{DefaultValues}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/DefaultValues.html}", note=[Accessed: 21-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_defaultvalues, organization={Wolfram Research}, title={DefaultValues}, year={2022}, url={https://reference.wolfram.com/language/ref/DefaultValues.html}, note=[Accessed: 21-November-2024 ]}