# Function that's \`not defined\` is defined

**URL:** <https://discourse.julialang.org/t/function-thats-not-defined-is-defined/50212>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 16, 2020, 6:04am UTC](https://discourse.julialang.org/t/function-thats-not-defined-is-defined/50212 "2020-11-16T06:04:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 16, 2020, 6:04am UTC](https://discourse.julialang.org/t/function-thats-not-defined-is-defined/50212/1 "2020-11-16T06:04:38Z")

</div>

The body of code is structured as

```julia
function my_fcn(...)
  ...
  if ...
    ...
    θ_sed(r) = ...
    return θ_sed, ...
  else
    ...
    t_sed(r) = ...
    @show t_sed(50.0)
    @show 2atan(t_sed(50.0))
    θ_sed(r) = 2atan(t_sed(r))
    @show θ_sed(50.0)
    return θ_sed, ...
  end
end

```

where the body of `else` results in the following error.

![image](https://global.discourse-cdn.com/julialang/original/3X/f/3/f3d4abb406b6696bd62e54080d130ba11708b0b1.png)

I don’t understand how `θ_sed` fails to be defined. I even evaluated its body before it, and it `@show`s that `2atan(t_sed(50.0))` evaluates without error. Additionally, the definition of `θ_sed` inside the `if` body evaluates just fine.

The weird thing is that when I remove the `if`-`else` structure and replace it with with the body of `else` like so

```julia
function my_fcn(...)
  ...
  t_sed(r) = ...
  @show t_sed(50.0)
  @show 2atan(t_sed(50.0))
  θ_sed(r) = 2atan(t_sed(r))
  @show θ_sed(50.0)
  return θ_sed, ...
end

```

it doesn’t evaluate an error, and `θ_sed` gets defined as a function without error.

What’s going on here? Why can’t I evaluate `θ_sed` inside the `else` body?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [November 16, 2020, 8:52am UTC](https://discourse.julialang.org/t/function-thats-not-defined-is-defined/50212/2 "2020-11-16T08:52:32Z")

</div>

You can’t conditionally define a function inside a function: [disallow methods of local functions in different blocks · Issue #15602 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15602). Solution: don’t do that

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 16, 2020, 9:07am UTC](https://discourse.julialang.org/t/function-thats-not-defined-is-defined/50212/3 "2020-11-16T09:07:16Z")

</div>

I suppose I’ll place the `if`-`else` inside the definition of my function, and do some closure.

---

<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:** [November 16, 2020, 2:36pm UTC](https://discourse.julialang.org/t/function-thats-not-defined-is-defined/50212/4 "2020-11-16T14:36:33Z")

</div>

You can conditionally define an _anonymous_ function without issues. For example, instead of

```julia
if y
  f(x) = ...

```

you can do

```julia
if y
  f = x -> ...

```
