# How to avoid capturing variables in functions?

**URL:** <https://discourse.julialang.org/t/how-to-avoid-capturing-variables-in-functions/24171>\
**Category:** New to Julia\
**Created:** [May 13, 2019, 11:38am UTC](https://discourse.julialang.org/t/how-to-avoid-capturing-variables-in-functions/24171 "2019-05-13T11:38:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [May 13, 2019, 11:38am UTC](https://discourse.julialang.org/t/how-to-avoid-capturing-variables-in-functions/24171/1 "2019-05-13T11:38:24Z")

</div>

How can I avoid creating closures? I noticed that due to the Julia syntax/REPL I inadvertently create a lot of closures. Consider my typical workflow where code evolves from a unstructured script towards a more function based interface: I have a bunch of lines that do what they are supposed to, and then I want to encapsulate the task and create a clean interface. When I place the lines in a function I repeatedly forget some variables that either should be arguments to the function or computed from the arguments. Below is a minimal example that runs and returns `5`:

```julia
function fun(x)
    a + x
end

a = 2

fun(3)

```

I forgot to make the `a` an argument. This runs fine now, but it will break in an other context. I thought the compiler should warn me about this? Isn’t this a step backwards?

Can I tell the function not to capture or the variable `a` not to be captured?

---

<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:** [May 13, 2019, 11:59am UTC](https://discourse.julialang.org/t/how-to-avoid-capturing-variables-in-functions/24171/2 "2019-05-13T11:59:53Z")

</div>

All you need to to is put your functions inside a `module`, which will prevent them from accessing globals from the REPL. Of course, any global variables inside the module will still be accessible, but those should be much easier to avoid.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [May 13, 2019, 12:09pm UTC](https://discourse.julialang.org/t/how-to-avoid-capturing-variables-in-functions/24171/3 "2019-05-13T12:09:42Z")

</div>

There is also [Traceur.jl](https://github.com/JunoLab/Traceur.jl) which can be used to detect issues like this.

```julia
julia> a = 7
7

julia> @trace fun(3)
┌ Warning: uses global variable Main.a
└ @ REPL[2]:1
┌ Warning: dynamic dispatch to Main.a + x
└ @ REPL[2]:1
┌ Warning: fun returns Any
└ @ REPL[2]:1
10

```

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [May 13, 2019, 12:16pm UTC](https://discourse.julialang.org/t/how-to-avoid-capturing-variables-in-functions/24171/4 "2019-05-13T12:16:53Z")

</div>

For the record, are you suggesting

```julia
module A
function fun(x)
    a + x
end
end

a = 2

using .A: fun

fun(3)

```

Followup: Is there something like `using .A: *` to bring all functions from a module? Or `export *` to export everything?

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [May 13, 2019, 3:27pm UTC](https://discourse.julialang.org/t/how-to-avoid-capturing-variables-in-functions/24171/5 "2019-05-13T15:27:40Z")

</div>

`using .A` should bring all exported functions into the current namespace. `export` requires explicit naming of exported symbols.

[https://docs.julialang.org/en/v1/manual/modules/](https://docs.julialang.org/en/v1/manual/modules/)

---

<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:** [May 13, 2019, 5:06pm UTC](https://discourse.julialang.org/t/how-to-avoid-capturing-variables-in-functions/24171/6 "2019-05-13T17:06:07Z")

</div>

Yeah, exactly. Running the code you provided results in:

```julia
julia> fun(3)
ERROR: UndefVarError: a not defined
Stacktrace:
 [1] fun(::Int64) at ./REPL[1]:3
 [2] top-level scope at none:0

```

which is presumably what you want in this case.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 13, 2019, 6:37pm UTC](https://discourse.julialang.org/t/how-to-avoid-capturing-variables-in-functions/24171/7 "2019-05-13T18:37:38Z")

</div>

No, I don’t think so:

```julia
julia> module A
       b=1
       end
Main.A

julia> using .A

julia> b
ERROR: UndefVarError: b not defined

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [May 13, 2019, 7:56pm UTC](https://discourse.julialang.org/t/how-to-avoid-capturing-variables-in-functions/24171/8 "2019-05-13T19:56:56Z")

</div>

You need to add `export b` to your module `A` for that to work.
