# Tutorial on closures?

**URL:** <https://discourse.julialang.org/t/tutorial-on-closures/62749>\
**Category:** General Usage\
**Tags:** tutorials, closure\
**Created:** [June 11, 2021, 4:07pm UTC](https://discourse.julialang.org/t/tutorial-on-closures/62749 "2021-06-11T16:07:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arrigo\_Benedetti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arrigo_benedetti/32/25545_2.png) [@Arrigo\_Benedetti](https://discourse.julialang.org/u/Arrigo_Benedetti)\
**Post date:** [June 11, 2021, 4:07pm UTC](https://discourse.julialang.org/t/tutorial-on-closures/62749/1 "2021-06-11T16:07:55Z")

</div>

I want to learn more about closures and their use with Julia. Does anyone recommend any paper/tutorial/book/blog post clearly explaining closures and what are their use cases for Julia programming? Most of the material that I have fond seems to be written for graduate students working in programming language theory…

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [June 11, 2021, 4:20pm UTC](https://discourse.julialang.org/t/tutorial-on-closures/62749/2 "2021-06-11T16:20:16Z")

</div>

This link [Functional JS #4: Closures. What are Closures and what are they… | by Krzysztof Czernek | DailyJS | Medium](https://medium.com/dailyjs/functional-js-4-closures-30b4ee8853c9) might help understand them, although it’s examples are javascript. The concepts are the same. Similarly in Go: [What is a Closure? - Calhoun.io](https://www.calhoun.io/what-is-a-closure/). I assume you’ve seen the brief mention of closures in the julia manual.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [June 11, 2021, 4:25pm UTC](https://discourse.julialang.org/t/tutorial-on-closures/62749/3 "2021-06-11T16:25:15Z")

</div>

I have a related answer on Stack Overflow that you could take a look at:

> <https://stackoverflow.com/questions/59920011/defining-functions-within-function-in-julia/59924678#59924678>

However, I should edit that answer. My first sentence is not quite correct:

> You usually only define a function inside another function if you want to return a closure.

Sometimes it is handy to define an inner function (closure) that you will only use inside the particular function that you’re in (without actually returning the closure). Not to mention that whenever you create a function in a local scope, it is actually a closure. So, for example, if you use `map(x) do ... end` inside a function, the `do` syntax is creating a closure.

Also, you can create closures with a `let` block:

```julia
julia> let
           y = 1
           global f(x) = x + y
       end
f (generic function with 1 method)

julia> f(2)
3

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 11, 2021, 6:54pm UTC](https://discourse.julialang.org/t/tutorial-on-closures/62749/4 "2021-06-11T18:54:36Z")

</div>

I have some notes on that: [https://m3g.github.io/JuliaNotes.jl/stable/closures/](https://m3g.github.io/JuliaNotes.jl/stable/closures/)

---

<div class="post-metadata">

**Author:** ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Post date:** [June 12, 2021, 8:49pm UTC](https://discourse.julialang.org/t/tutorial-on-closures/62749/5 "2021-06-12T20:49:41Z")

</div>

Very helpful.

Are closures typically used for small functions like the examples? Would anyone, say, return a 500 line function as a closure?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 12, 2021, 9:07pm UTC](https://discourse.julialang.org/t/tutorial-on-closures/62749/6 "2021-06-12T21:07:59Z")

</div>

Not neessarily. You can use to closure just to pass parameters to a more complicated function:

```julia
julia> v = rand(10);

julia> function f(x)
         if x < 0.5
           true
         else
           false
         end
       end
f (generic function with 2 methods)

julia> findall(x -> f(x), v)
6-element Vector{Int64}:
 2
 5
 6
 7
 8
 9

```

Or there is where the `do` syntax appears:

```julia
julia> findall(v) do x
         if x < 0.5
           true
         else
           false
         end
       end
2-element Vector{Int64}:
 5
 8

```

where that `do x <body>` means `x -> <body>` and is passed as the first argument of `findall` (which is omitted there.
