# Closure strange behaviour

**URL:** <https://discourse.julialang.org/t/closure-strange-behaviour/53052>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 8, 2021, 4:54pm UTC](https://discourse.julialang.org/t/closure-strange-behaviour/53052 "2021-01-08T16:54:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![claudio20497](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/claudio20497/32/36602_2.png) [@claudio20497](https://discourse.julialang.org/u/claudio20497)\
**Post date:** [January 8, 2021, 4:54pm UTC](https://discourse.julialang.org/t/closure-strange-behaviour/53052/1 "2021-01-08T16:54:14Z")

</div>

Hello,

I’m experiencing the following unexpected behaviour. Imagine you have a function like:

```julia
function select_function(p)
    
    if p == "a"
        
        function a()
            println("a")
        end
        
    elseif p == "b"
        
        function a()
             println("b")
        end
        
    end
end

```

I would expect that :

```julia
select_function("a")()

```

```julia
a

```

Instead I get:

```julia
select_function("a")()

```

```julia
b

```

Moreover, I get the following error:

```julia
select_function("b")()

```

```julia
UndefVarError: a not defined

Stacktrace:
 [1] select_function(::String) at .\In[9]:11
 [2] top-level scope at In[13]:1
 [3] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

**May someone explain the logic behind it?**

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [January 8, 2021, 4:59pm UTC](https://discourse.julialang.org/t/closure-strange-behaviour/53052/2 "2021-01-08T16:59:56Z")

</div>

See [Defining a function inside if...else..end NOT as expected?](https://discourse.julialang.org/t/defining-a-function-inside-if-else-end-not-as-expected/13815)
