# Return a function defined in a function

**URL:** <https://discourse.julialang.org/t/return-a-function-defined-in-a-function/21849>\
**Category:** New to Julia\
**Created:** [March 14, 2019, 2:33am UTC](https://discourse.julialang.org/t/return-a-function-defined-in-a-function/21849 "2019-03-14T02:33:31Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 14, 2019, 2:53am UTC](https://discourse.julialang.org/t/return-a-function-defined-in-a-function/21849/2 "2019-03-14T02:53:37Z")

</div>

It’s an unfortunate and long-standing bug. See: [Confused by behaviour of nested functions](https://discourse.julialang.org/t/confused-by-behaviour-of-nested-functions/20576) for more discussion.

Fortunately, it’s easy to avoid by defining an anonymous function instead:

```julia
function fx(n)
  if n == 1
    f = x -> 2x
  else
    f = x -> 3x
  end
  f
end

```

---

_[View the full topic](https://discourse.julialang.org/t/return-a-function-defined-in-a-function/21849)._
