Inspired by this and this question (and how I handle this in practice), what is the best way to default a function value when a certain condition is met?

For example, if a function is defined as:

func[x_] := (x - 1)^2 + 5

and for values of x less than 0, x should equal zero. I also always want the function to evaluate even when x is less than 0. Normally, I would handle as such:

func2[x_] := Module[{xx = x},
   If[xx < 0, xx = 0];
   (xx - 1)^2 + 5]

If I try to use patterns to define the conditions, I have to define the function twice for each region:

func3[x_?Positive] := (x - 1)^2 + 5
func3[x_] := 6

I've had no luck using other patterns to define the function.

While this is an admittedly easy example that can be easily handled using Max[x,0], etc., patterns can be much more complex such as "integers divisible by a prime number".

So, can a pattern married with a default argument handle this situation? And what is the most efficient on a computation and memory basis?


Here is a more complicated version of a function to which I referred to in the comments:

func[qi_, dei_, b_, dmin_, rt_, pt_, t_] := 
   Module[{ptt = Max[pt, 0], rtt = Max[rt, 0], di, diexp, xotime, xorate},
    di = 1/b*((1 - dei)^-b - 1)/365;
    diexp = -Log[1 - dmin]/365;
    xotime = (di - diexp)/(di*b*diexp) + rtt + ptt;
    xorate = qi*(1 + b*di*(xotime - ptt - rtt))^(-1/b); 
    Piecewise[{{qi/2 (1 + t/rtt), t <= rtt}, {qi, rtt < t <= (rtt + ptt)},
        {qi*(1 + b*di*(t - rtt - ptt))^(-1/b), (rtt + ptt) < t <= xotime},
        {xorate*Exp[-diexp*(t - xotime)], t > xotime}}]

I'll plug in random variates (which can sometimes be zero based on the distribution), and evaluate this function multiple times. However, rt and pt should never be negative and should default to zero. In other words, have minimum value of zero. While the way I have it works well, the original question stemmed from the idea that this could be done more efficient with a pattern. jVincent's vanishing patterns solution works only if one argument has this pattern. If both rt and pt have this pattern, the vanishing pattern doesn't seem to work.

  • $\begingroup$ Can't you use Min[x,0] as func[x_] := (Min[x, 0] - 1)^2 + 5 ? $\endgroup$ Feb 12, 2013 at 14:14
  • $\begingroup$ @b.gatessucks Sure, in this situation, but I was looking for a more comprehensive solution for other conditionals (between values, integers, divisible by 71, etc...), that I can expand on later. And it would be Max in this case. $\endgroup$
    – kale
    Feb 12, 2013 at 14:16
  • $\begingroup$ You're right, thanks. $\endgroup$ Feb 12, 2013 at 14:19
  • $\begingroup$ func[x_]=(x - 1)^2 + 5; f[x_] = Piecewise[{{func[x], x >= 0}}, func[0]] $\endgroup$
    – ssch
    Feb 12, 2013 at 14:35
  • $\begingroup$ This doesn't work, since optional doesn't work like that, but if I understand you, you are looking for something like func[(_?Negative | x_: 0)] := (x - 1)^2 + 5 where x is matched as zero whenever it's negative and as it's actual match if positive? $\endgroup$
    – jVincent
    Feb 12, 2013 at 14:37

4 Answers 4


I tried my hand at this problem before reading any of the answers and I arrived as jVincent's second solution too. You stated in a comment:

Doesn't seem to work for functions that have multiple arguments which should match these patterns though.

You should add an example of your more complicated function so that this might be addressed. Also, clarify if you mean multiple default arguments or simply multiple arguments. Here is an example of this method applied to a function with multiple arguments:


func[x : (_?Positive | 0) : 0, ___, y_] := {x, y}

func[#, 2] & /@ {-3, 0, 5}
{{0, 2}, {0, 2}, {5, 2}}

Incidentally your specific example lends itself to a nice little trick using what I call "vanishing patterns" and the Head surrounding x:


func[x_ | _] /; x > 0 := (x - 1)^2 + 5

func /@ {-3, 0, 5}
{6, 6, 21}

When the pattern x_ (under the Condition) does not match anything but an alternative pattern does, x in the RHS is replaced with an empty sequence. If we look at the FullForm of your RHS expression we see:


Therefore if x is removed it behaves the same as if x were zero.

Responding to your update here is a definition for your many-parameter function:


func[qi_, dei_, b_, dmin_, rt : (_?Positive | 0) : 0,
   pt : (_?Positive | 0) : 0, ___, t_] := {qi, dei, b, dmin, rt, pt, t}

func[1, 2, 3, 4, -3, -7, 5]
{1, 2, 3, 4, 0, 0, 5}
func[1, 2, 3, 4, 3, 7, 5]
{1, 2, 3, 4, 3, 7, 5}

Let me be clear that this is all just for fun to me and in any serious application I would likely use some variation of Szabolcs's method, e.g.:

func[qi_, dei_, b_, dmin_, rt_, pt_, t_] := 
 func[{qi, dei, b, dmin, Max[0, rt], Max[0, pt], t}]

func[{qi_, dei_, b_, dmin_, rt_, pt_, t_}] := (* main def *)
  • $\begingroup$ See update for the more complicated function. rt and pt are the two values that should default to zero if the specified values are less than 0. $\endgroup$
    – kale
    Feb 21, 2013 at 16:26
  • $\begingroup$ Oh, and rt and pt are used later for the Piecewise function. If the vanishing pattern is used, I don't think these conditionals work. $\endgroup$
    – kale
    Feb 21, 2013 at 16:29
  • $\begingroup$ @kale I added an example to my answer, and I note of my intention. $\endgroup$
    – Mr.Wizard
    Feb 21, 2013 at 17:13
  • $\begingroup$ So your last paragraph is what I was really going for: "Yes you can do this but you should do this. As far as the variation of Szabolcs's vs my Module[rt=Max[rt,0] is there any benefit to one over the other? $\endgroup$
    – kale
    Feb 21, 2013 at 19:27
  • $\begingroup$ @kale Well, why didn't you say that in the first place? ;-) No, I am not aware of any method within single-definition pattern matching that is appropriate for production code. I saw this question as in interesting exploration of possible exploits. A bit later I'll add some thoughts about additional DownValues versus scoping. $\endgroup$
    – Mr.Wizard
    Feb 22, 2013 at 0:14

I would use

f[x_?Negative] := f[0]
f[x_] := (x - 1)^2 + 5

or just Piecewise.

  • $\begingroup$ Or equivalently f[x_] := f[0];f[x_?NonNegative] := (x - 1)^2 + 5 but I think that's what the OP wants to avoid. $\endgroup$
    – sebhofer
    Feb 12, 2013 at 15:16
  • $\begingroup$ @sebhofer Maybe I didn't understand the question well, I thought he wanted to avoid writing out the definition (x - 1)^2 + 5 (or a special case of it) several times. I avoided that by "redirecting" to f[0]. I know I cheated by using that fact that 0 is not negative ... $\endgroup$
    – Szabolcs
    Feb 12, 2013 at 15:19
  • $\begingroup$ Maybe you're right... I was wondering the same. I thought he tried to avoid defining two distinct DownValues. Anyway +1 because that would have been my answer too. $\endgroup$
    – sebhofer
    Feb 12, 2013 at 15:23
  • $\begingroup$ Hi, inspired by your suggestion I ve tried something simple of the type: g[q_,p_?p_<q_]:=0, and g[q_,p_]:=4; but when i test it for inputs with p smaller than q, it still returns 4. Am I expressing the condition wrongly here? $\endgroup$
    – user21766
    Jul 14, 2017 at 13:50
  • $\begingroup$ @user929304 Yes, lots of mistakes in there ... Try g[q_, p_] /; p < q := 0. 1. Look up the difference between ? (PatternTest) and /; (Condition). 2. p_ (with underscore) is a pattern, and p (without underscore) refers to the expression that pattern matched 3. If you refer to both q and p on the RHS of /; then both q_ and p_ must be present on the LHS. Thus the condition needed to be moved outside of g. $\endgroup$
    – Szabolcs
    Jul 15, 2017 at 6:37

I'll add this as a different answer since it's quite different from my first alternative method. It seems that you can in special cases at least get by with:

func[x : (_?Positive | 0) : 0, dummy___?Negative] := (x - 1)^2 + 5

The working principle is that a dummy argument pattern is added, which matches the negative case. When supplied with a negative input, the first pattern fails, but the second will expand and match the input, letting x be set to the default.

  • $\begingroup$ Very sneaky! I like it :) $\endgroup$
    – sebhofer
    Feb 12, 2013 at 16:25
  • $\begingroup$ nice!! (+1) also works without Alternatives: func[x : _?NonNegative : 0, dummy___?Negative] := (x - 1)^2 + 5? $\endgroup$
    – kglr
    Feb 12, 2013 at 19:18
  • $\begingroup$ Wow. My head is spinning on this one. Doesn't seem to work for functions that have multiple arguments which should match these patterns though. Impressive, nonetheless. $\endgroup$
    – kale
    Feb 12, 2013 at 19:27

It seems like this sort of matching isn't possible. Personally I have, at times, used a method of using Alternatives to circumvent the pattern, such that it's bound to Sequence[], and then providing the default values on the right hand side:

def[a_, ___] := a
func[(x_?Positive | _)] := (def[x, 0] - 1)^2 + 5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.