# \[?\] Define Function using "Total\[\]"?

**URL:** <https://community.wolfram.com/t/define-function-using-total/10457>\
**Category:** Questions\
**Created:** [May 2, 2017, 5:44pm UTC](https://community.wolfram.com/t/define-function-using-total/10457 "2017-05-02T17:44:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbreitba](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@mbreitba](https://community.wolfram.com/u/mbreitba)\
**Post date:** [May 2, 2017, 5:44pm UTC](https://community.wolfram.com/t/define-function-using-total/10457/1 "2017-05-02T17:44:36Z")

</div>

Using Mathematica 11, I want to define a piece wise function, using `Total` and `Select`, in the following way:

```
g[x_] := Total[Select[{
     {15, 4},
     {10, 2},
     {5, 3},
     {0, 1}
     }, #[[1]] < x &][[All, 2]]]

```

Unfortunately, g does not behave as i expect, for example

```
g[2]
g[12]
g[x]
g[x]/.{x->12}

```

returns

```
1
6
0
0

```

and not as expected

```
1
6
g[x]
6

```

I.e. when inserting numbers directly as an argument, the function works properly, but using a symbol as argument and assigning a value using a rule fails. This is not the case for any other function I define (e.g. using `Piecewise`). Is there a way to fix this, or is my method of defining a piece wise function unsuitable? Of course I could simply rewrite it using the `Piecewise` function, but I hope to get a deeper understanding of Mathematica mechanics investigating this problem.

Any ideas?

Thanks and regards,  
Moritz B

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [May 2, 2017, 6:17pm UTC](https://community.wolfram.com/t/define-function-using-total/10457/2 "2017-05-02T18:17:36Z")

</div>

Select only picks elements for which the criterion **evaluates to true.** If is is unevaluated, or False it will not be selected. So if you do ‘x’, all the comparisons will not evaluate to true, but to left unevaluated. So therefore, Select will not pick anything and just return an empty list. The Total of an empty list is 0. This is normal behavior of Select:

```
Select[{{15, 4}, {10, 2}, {5, 3}, {0, 1}}, #[[1]] < x &]

```

returns {} because (first example):

```
15 < x

```

does not evaluate to True.

---

<div class="post-metadata">

**Author:** ![carlw](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/carlw/32/20872_2.png) [@carlw](https://community.wolfram.com/u/carlw)\
**Post date:** [May 2, 2017, 8:30pm UTC](https://community.wolfram.com/t/define-function-using-total/10457/3 "2017-05-02T20:30:37Z")

</div>

Use

```
g[x_?NumberQ] := Total[Select[{
     {15, 4},
     {10, 2},
     {5, 3},
     {0, 1}
     }, #[[1]] < x &][[All, 2]]]

```

instead.

---

<div class="post-metadata">

**Author:** ![mbreitba](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@mbreitba](https://community.wolfram.com/u/mbreitba)\
**Post date:** [May 3, 2017, 9:50am UTC](https://community.wolfram.com/t/define-function-using-total/10457/4 "2017-05-03T09:50:43Z")

</div>

Thanks Carl Woll, that did the job! Also thanks for the explanation Sander Huisman!
