# Are functions the same as closures in this case?

**URL:** <https://discourse.julialang.org/t/are-functions-the-same-as-closures-in-this-case/32467>\
**Category:** Performance\
**Tags:** closure\
**Created:** [December 19, 2019, 1:28am UTC](https://discourse.julialang.org/t/are-functions-the-same-as-closures-in-this-case/32467 "2019-12-19T01:28:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [December 19, 2019, 1:28am UTC](https://discourse.julialang.org/t/are-functions-the-same-as-closures-in-this-case/32467/1 "2019-12-19T01:28:55Z")

</div>

Are these functions/closures examples:

```Julia
function main()

  a = 1.0
  b = 5.0

  function f1(t)
    return a * t + b
  end

  f2(t) = a * t + b

  f3 = t -> a * t + b

  solver(f1, f2, f3)
  return nothing
end

```

the same with respect to performance?

Is this correct:

1. f1 is a function.
2. f2 is a function (?)
3. f3 is a closure.

In the solver function, if we evaluate each function, they will have the same performance?

Thanks!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 19, 2019, 2:04am UTC](https://discourse.julialang.org/t/are-functions-the-same-as-closures-in-this-case/32467/2 "2019-12-19T02:04:59Z")

</div>

All of these are closures.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 19, 2019, 2:06am UTC](https://discourse.julialang.org/t/are-functions-the-same-as-closures-in-this-case/32467/3 "2019-12-19T02:06:21Z")

</div>

And they are all functions too (so `f isa Function` will give true), just different syntax to do the same thing.

---

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [December 19, 2019, 2:09am UTC](https://discourse.julialang.org/t/are-functions-the-same-as-closures-in-this-case/32467/4 "2019-12-19T02:09:23Z")

</div>

okay, but a closure builds a `struct` with captured variables as elements, while I do not know what a function does.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 19, 2019, 2:16am UTC](https://discourse.julialang.org/t/are-functions-the-same-as-closures-in-this-case/32467/5 "2019-12-19T02:16:29Z")

</div>

Each function is an instance of its own type (a subtype of `Function`), so `log isa typeof(log)` is true and `typeof(log) <: Function` is also true. `Function` is an abstract type in Julia. Structs can be made subtypes of `Function` and can be made callable using the syntax:

```julia
struct MyStruct <: Function end
(s::MyStruct)(args...) = println(args...)

```

`s = MyStruct(); s(1)` is equivalent to `println(1)`.

I don’t know if this answers your question, but others might be able to comment better on lowering and compilation techniques used for closures and functions.

---

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [December 19, 2019, 2:30am UTC](https://discourse.julialang.org/t/are-functions-the-same-as-closures-in-this-case/32467/6 "2019-12-19T02:30:37Z")

</div>

This is good enough!

As always, thanks a lot for your help!

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [December 19, 2019, 4:02am UTC](https://discourse.julialang.org/t/are-functions-the-same-as-closures-in-this-case/32467/7 "2019-12-19T04:02:06Z")

</div>

> [@rvignolo](#):
>
> okay, but a closure builds a `struct` with captured variables as elements, while I do not know what a function does.

From a programming language theory perspective the issue is how to resolve free variables in the body of a function.

```julia
function f1(t)
   return a * t + b
end

```

has 2 free variables, a and b. But it was defined in a context where those free variables were bound, and so a closure _captures_ those bindings into a structure.

For a function defined at the “toplevel”, then you can resolve the free variables as global variables. So in some sense a function at toplevel is a closure whose bindings are the toplevel bindings.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 19, 2019, 4:02am UTC](https://discourse.julialang.org/t/are-functions-the-same-as-closures-in-this-case/32467/8 "2019-12-19T04:02:47Z")

</div>

> [@rvignolo](#):
>
> okay, but a closure builds a `struct` with captured variables as elements, while I do not know what a function does.

A non-closure function is also a `struct` with captured variables as fields. It’s just has zero fields.

---

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [December 19, 2019, 12:28pm UTC](https://discourse.julialang.org/t/are-functions-the-same-as-closures-in-this-case/32467/9 "2019-12-19T12:28:10Z")

</div>

Thanks @yuyichao and @dlakelan!
