# Help with defining a function

**URL:** <https://community.wolfram.com/t/help-with-defining-a-function/2267>\
**Category:** Questions\
**Tags:** mathematica, calculus\
**Created:** [May 24, 2014, 7:09pm UTC](https://community.wolfram.com/t/help-with-defining-a-function/2267 "2014-05-24T19:09:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fell](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fell](https://community.wolfram.com/u/fell)\
**Post date:** [May 24, 2014, 7:09pm UTC](https://community.wolfram.com/t/help-with-defining-a-function/2267/1 "2014-05-24T19:09:46Z")

</div>

Here is a simple example of the problem I cannot solve.

```auto
In[4]:= MyInt[f_[x_]] := Integrate[f[x], x]

In[6]:= f[x] := x^3;

In[7]:= MyInt[f[x]]
Out[7]= MyInt[x^3]

```

If anyone can tell my why I do not get the integration to work I would be most grateful.

Many thanks,  
Dick Fell

---

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [May 24, 2014, 7:17pm UTC](https://community.wolfram.com/t/help-with-defining-a-function/2267/2 "2014-05-24T19:17:40Z")

</div>

```auto
In[33]:= MyInt[f_] := Integrate[f[x], x]

In[34]:= MyInt[#^3 &]

Out[34]= x^4/4

```

---

<div class="post-metadata">

**Author:** ![seanc](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/seanc/32/34005_2.png) [@seanc](https://community.wolfram.com/u/seanc)\
**Post date:** [May 24, 2014, 8:19pm UTC](https://community.wolfram.com/t/help-with-defining-a-function/2267/3 "2014-05-24T20:19:16Z")

</div>

The issue is with the order in which things will evaluate. You can use Trace to see the order that things will evaluate in.

I don’t reccomend programming this way if avoidable. Changing the order of evaluation for things can quickly make code difficult to read.

What you want to do is prevent the argument to MyInt from evaluating before it is used. Functions which do this have the attribute “HoldFirst” or “HoldAll”

```auto
SetAttributes[MyInt, HoldAll]
MyInt[f_[x_]] := Integrate[f[x], x];
f[x] := x^3;

```

---

<div class="post-metadata">

**Author:** ![fell](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@fell](https://community.wolfram.com/u/fell)\
**Post date:** [May 25, 2014, 7:36pm UTC](https://community.wolfram.com/t/help-with-defining-a-function/2267/4 "2014-05-25T19:36:26Z")

</div>

Thank you both for your answers.

If you do not mind, would you comment on why this does not work? Clearly, I am still confused about Mathematica’s evaluation process.

In[8]:= SetAttributes[MyResidue, HoldAll]  
In[9]:= MyResidue[f\_, pole] := Residue[f, {p0, pole}]

In[10]:= f := 1/(p0^2 - (wp - I\*e1)^2)

In[11]:= MyResidue[f, -wp + I\*e1]

Out[11]= MyResidue[f, -wp + I e1]

In[12]:= Trace[MyResidue[f, -wp + I\*e1]]

Out[12]= {}

Thanks

---

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [May 25, 2014, 8:10pm UTC](https://community.wolfram.com/t/help-with-defining-a-function/2267/5 "2014-05-25T20:10:15Z")

</div>

```auto
In[16]:= MyInt[f_[x_]] := Integrate[f[x], x]

In[17]:= f[x] := x^3;

In[18]:= MyInt[f[x]] // Trace

Out[18]= {{\!\(\*
TagBox[
RowBox[{"f", "[", "x", "]"}],
HoldForm]\), \!\(\*
TagBox[
SuperscriptBox["x", "3"],
HoldForm]\)}, \!\(\*
TagBox[
RowBox[{"MyInt", "[", 
SuperscriptBox["x", "3"], "]"}],
HoldForm]\)}

```

Shows that f was replaced by x^3 in the first step, so MyInt[x^3] didn’t pattern match to the definition of MyInt
