The Stack Overflow podcast is back! Listen to an interview with our new CEO.

# All Questions

31 questions
Filter by
Sorted by
Tagged with
6answers
5k views

### How to avoid collision between optional arguments and options

Is it possible to create a function with optional arguments that also takes options? Here is a simple example. I have a function f with option "g". It also has ...
5answers
2k views

### Can I make a default for an optional argument the value of another argument?

I'd like to define a function with several optional arguments, some of which default to the value supplied for other arguments. For example, I'd like to be able to write something like ...
3answers
2k views

### Is there any way to define pure functions with optional arguments?

For example consider the following function: Function[{u,v},u^2+v^4] Is there anyway to define default values for the variables ...
2answers
2k views

### How to Combine Pattern Constraints and Default Values for Function Arguments?

EDIT: As several respondents have noted in the answers and comments below, the original example had a default value that would never be used because of the way patterns and default values are ...
4answers
554 views

### Skipping “middle” Optional Arguments

Is there a way to call a function where the "middle" arguments should use their default value while specifying values for the "right-most" arguments? For example, given this function: ...
4answers
602 views

### Optional argument that can be completely omitted?

I am writing a simple function return similar words. Clear[similarWords] similarWords[string_]:=Nearest[WordList[],string] I want to add another argument ...
3answers
281 views

### Specifying optional arguments with variables

Perhaps this is poor programming on my part, but I would like to create a function that uses a variable as an optional parameter: var = 2; f[a_:var] := {a} The ...
4answers
114 views

### How to add Optional Sequence

Note: This question could be a duplicate but I couldn't find any so far. It is known that Optional (:) can be use to represent expression if omitted. ...
1answer
101 views

### Except @ OptionsPattern[] affects OptionValue

The background This example works as expected, small function with a special case rule: ...
1answer
551 views

### define a function with optional arguments

I have defined a function as follows: ...
2answers
396 views

### Optional arguments with lists as function parameter

I'm developing a function f which should support optional arguments {x_, y_} in list form. A simple example looks like this: <...
2answers
183 views

### Change some option of a function when calling it [duplicate]

Say I have defined some function like f[p_] := Plot[Sin[p*x], {x, 0, 2 Pi}] Now in most cases, I just need to specify the value of ...
1answer
159 views

### A question about two ways to use Default

Usually we define a function with default values by the syntax x_:default, but there is another technique to do this by registering a global default value with <...
2answers
95 views

### How to write a HeldOptional variant of Optional that does not evaluate its second argument?

How to specify optional arguments that take functional values made me wonder: Can we come up with a variant of Optional that allows to do the following: ...
2answers
87 views

### How to prevent an Optional parameter from taking an OptionsPattern parameter without multiple function definitions? [duplicate]

I have function the needs to enable some Optional parameters and an OptionsPattern. The issue I am facing is that the options ...
4answers
186 views

### How to specify optional arguments that take functional values

I am trying to make a function with an optional argument that may change but mathematica caches the value when the function is defined. Here is an example where the optional time variable is set when ...
2answers
63 views

### How to define a function with an optional value related to the arguments?

It's known that we can easily define a function with optional value which is not related with other arguments, but my question is how can I define a function with an optional value which is really ...
1answer
171 views

### How can I define a function taking two distinguishable sets of options?

It commonly happens that one wants to define some function foo which in turn calls some other function (e.g., Plot) that can ...
0answers
92 views

### Function of arguments given in non-fixed order [duplicate]

normally we define functions as f[x_, y_, z_] := ArcSin[x^2*y/z] however when one has many arguments (as I do) I find difficult to remember what is the meaning ...
2answers
131 views

### Plot does not take range with OptionValue and PlotRange option

I am making a function that is to use Show to overlay a few plots of a function. I want the first Plot in ...
1answer
110 views

...
1answer
121 views

### Is there a shorthand for Optional with Condition?

I have a recursive function that does some work to an object for a repeatedamount of times. ...
0answers
30 views

### Scoping of OptionValue for inner functions in closures

Assume a function f is a closure over an inner function g. Both f and ...
0answers
106 views

### How to create a function with option that shows dropdownlist like ( for example ) Manipulate with ControlPlacement? [duplicate]

Consider the Mathematica function Manipulate where you assign the placement of the controls with the option parameter ...
0answers
100 views

### How can this function's options be used in different places? [duplicate]

I wrote this function to make a ListPlot where the points are colored by a third argument. ...
3answers
2k views

### How to combine multiple functions into a single function, indexed bya new argument

I have a system of expressions (matrices) which correspond to increasing orders of precision in a theory. These expressions are quite complex (require about 40+ lines of code to produce) and are not ...
2answers
195 views

### How to deal with the condition that a function own many options?

Assming that I have a function myFunc which has some options. ...
1answer
39 views

### Why doesn't OptionValue cooperate with Map? [duplicate]

Consider the following definition: Options[f] = {a -> 1, b -> 2} f[x_,OptionsPattern[]] := {x,OptionValue/@{a,b}} I thought that doing something like ...
1answer
91 views

### User-defined density plotting function with a functional argument: extracting variables from the function and plotting in a circular region

I would like to customize the function DensityPlot and use it in a package as a function called MyDensityPlot: The functions ...
0answers
47 views

### What is the best way to pass on options? [duplicate]

Say I have two functions which take the same set of options, e.g. Options[f1] = {a -> 1}; Options[f2] = {a -> 1}; Now, ...
0answers
37 views

### How to use OptionValue inside a pure function [duplicate]

I want to define a function with OptionsPattern as argument which returns a pure function using OptionValue: ...