# Use function inside compile

**URL:** <https://community.wolfram.com/t/use-function-inside-compile/5902>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [June 9, 2015, 5:35am UTC](https://community.wolfram.com/t/use-function-inside-compile/5902 "2015-06-09T05:35:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![boristhphys](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/boristhphys/32/3064_2.png) [@boristhphys](https://community.wolfram.com/u/boristhphys)\
**Post date:** [June 9, 2015, 5:35am UTC](https://community.wolfram.com/t/use-function-inside-compile/5902/1 "2015-06-09T05:35:29Z")

</div>

I noticed that inside Compile there seems to be no way to define a function. It always call MainEvalute:

```
fun = Compile[{x, _Real}, Module[{y = x, f}, f[y_] = y^2; y + f[y]]]

```

Any way to overcome it?

Thanks.

---

<div class="post-metadata">

**Author:** ![cadrin](https://avatars.discourse-cdn.com/v4/letter/c/c5a1d2/32.png) [@cadrin](https://community.wolfram.com/u/cadrin)\
**Post date:** [June 9, 2015, 7:53pm UTC](https://community.wolfram.com/t/use-function-inside-compile/5902/2 "2015-06-09T19:53:15Z")

</div>

![7284re07.png](https://global.discourse-cdn.com/wolfram/original/3X/d/d/dd3be0f891ac7724eb0eaea75cb9ad1d18a973b8.png)

---

<div class="post-metadata">

**Author:** ![boristhphys](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/boristhphys/32/3064_2.png) [@boristhphys](https://community.wolfram.com/u/boristhphys)\
**Post date:** [June 9, 2015, 8:15pm UTC](https://community.wolfram.com/t/use-function-inside-compile/5902/3 "2015-06-09T20:15:57Z")

</div>

The point is not just to use Module to get the answer, I need to make multiple function assignments inside `Module` and it should be compiled by function `Compile` to ensure a fast execution as it needs to be called hundreds thousands of times.

---

<div class="post-metadata">

**Author:** ![cadrin](https://avatars.discourse-cdn.com/v4/letter/c/c5a1d2/32.png) [@cadrin](https://community.wolfram.com/u/cadrin)\
**Post date:** [June 9, 2015, 8:43pm UTC](https://community.wolfram.com/t/use-function-inside-compile/5902/4 "2015-06-09T20:43:49Z")

</div>

![6304re08.png](https://global.discourse-cdn.com/wolfram/original/3X/3/7/373bb6e31afc4822b433ff6468277b6d978c1524.png)

---

<div class="post-metadata">

**Author:** ![boristhphys](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/boristhphys/32/3064_2.png) [@boristhphys](https://community.wolfram.com/u/boristhphys)\
**Post date:** [June 9, 2015, 8:45pm UTC](https://community.wolfram.com/t/use-function-inside-compile/5902/5 "2015-06-09T20:45:32Z")

</div>

I don’t see how that answers the question.

---

<div class="post-metadata">

**Author:** ![cadrin](https://avatars.discourse-cdn.com/v4/letter/c/c5a1d2/32.png) [@cadrin](https://community.wolfram.com/u/cadrin)\
**Post date:** [June 9, 2015, 9:11pm UTC](https://community.wolfram.com/t/use-function-inside-compile/5902/6 "2015-06-09T21:11:17Z")

</div>

![re09.png](https://global.discourse-cdn.com/wolfram/original/3X/1/7/17bd7db81e9ec75c499239a95cdf43b7afc8a287.png)
