# Same code in if and else blocks, get UndefVarError only on else condition

**URL:** <https://discourse.julialang.org/t/same-code-in-if-and-else-blocks-get-undefvarerror-only-on-else-condition/11611>\
**Category:** General Usage\
**Tags:** scope\
**Created:** [June 12, 2018, 2:51pm UTC](https://discourse.julialang.org/t/same-code-in-if-and-else-blocks-get-undefvarerror-only-on-else-condition/11611 "2018-06-12T14:51:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwolters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mwolters/32/8696_2.png) [@mwolters](https://discourse.julialang.org/u/mwolters)\
**Post date:** [June 12, 2018, 2:51pm UTC](https://discourse.julialang.org/t/same-code-in-if-and-else-blocks-get-undefvarerror-only-on-else-condition/11611/1 "2018-06-12T14:51:07Z")

</div>

Suppose I have a function with 2 arguments, and I want to make a new function with one of the arguments fixed to a value, depending on some condition. For example:

```julia
fun(x,a) = a*x
function foo(flag::Bool)
   if flag
       bar(x) = fun(x,2)
   else
       bar(x) = fun(x,2)
   end
   return bar
end

```

In this example the value of `a` is fixed to `2` on both branches, just for illustration.  
If I run this code in a script, `foo(true)` returns the desired function, but `foo(false)` gives

```julia
ERROR: UndefVarError: bar not defined
Stacktrace:
 [1] foo(::Bool) at .\none:7
 [2] eval(::Module, ::Any) at .\boot.jl:235

```

I realize now at least a couple of different ways to re-write `foo` so that it works, but my question is **why** does the above version not work when control flows down the `else` branch? I think I can learn something by understanding this, but I can’t make sense of it in terms of my understanding of scoping rules.

Thank you (I’m using Julia 0.6.2, BTW)

---

<div class="post-metadata">

**Author:** ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)\
**Post date:** [June 12, 2018, 3:08pm UTC](https://discourse.julialang.org/t/same-code-in-if-and-else-blocks-get-undefvarerror-only-on-else-condition/11611/2 "2018-06-12T15:08:30Z")

</div>

You cannot define a function conditionally in this way because the function method table needs to be constructed at the top level. [disallow methods of local functions in different blocks · Issue #15602 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15602) has an explanation and some discussion of workarounds. For your purpose you should probably make bar an anonymous function like this:

```julia
bar = x -> fun(x, 2)

```

---

<div class="post-metadata">

**Author:** ![mwolters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mwolters/32/8696_2.png) [@mwolters](https://discourse.julialang.org/u/mwolters)\
**Post date:** [June 13, 2018, 2:30am UTC](https://discourse.julialang.org/t/same-code-in-if-and-else-blocks-get-undefvarerror-only-on-else-condition/11611/3 "2018-06-13T02:30:10Z")

</div>

Thank you and sorry for duplicating the issue you linked. If this type of conditional function definition is not allowed in Julia, it should probably produce a (more informative) error in both the `true` and `false ` cases. Since that issue is still open, I assume better handling of this problem (and some corresponding documentation) will appear in due course.

---

<div class="post-metadata">

**Author:** ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)\
**Post date:** [June 13, 2018, 10:11am UTC](https://discourse.julialang.org/t/same-code-in-if-and-else-blocks-get-undefvarerror-only-on-else-condition/11611/4 "2018-06-13T10:11:42Z")

</div>

Yes, though the quickest way to make the documentation appear is probably to add it yourself 🙂 It may be worth adding a note in the [Conditional Evaluation](https://docs.julialang.org/en/latest/manual/control-flow/#man-conditional-evaluation-1) or [Functions](https://docs.julialang.org/en/latest/manual/functions/) section of the manual until the root problem is fixed or a better error/warning is added.
