# Weird behaviour with function variables leaking out of if blocks

**URL:** <https://discourse.julialang.org/t/weird-behaviour-with-function-variables-leaking-out-of-if-blocks/72651>\
**Category:** New to Julia\
**Tags:** question, scope, function\
**Created:** [December 6, 2021, 12:32pm UTC](https://discourse.julialang.org/t/weird-behaviour-with-function-variables-leaking-out-of-if-blocks/72651 "2021-12-06T12:32:31Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 6, 2021, 12:36pm UTC](https://discourse.julialang.org/t/weird-behaviour-with-function-variables-leaking-out-of-if-blocks/72651/2 "2021-12-06T12:36:31Z")

</div>

> [@Same code in if and else blocks, get UndefVarError only on else condition](https://discourse.julialang.org/t/same-code-in-if-and-else-blocks-get-undefvarerror-only-on-else-condition/11611):
>
> 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: 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 ERROR: UndefVarErr…

---

_[View the full topic](https://discourse.julialang.org/t/weird-behaviour-with-function-variables-leaking-out-of-if-blocks/72651)._
