# Global variables / anonymous function

**URL:** <https://discourse.julialang.org/t/global-variables-anonymous-function/26461>\
**Category:** New to Julia\
**Created:** [July 17, 2019, 12:57pm UTC](https://discourse.julialang.org/t/global-variables-anonymous-function/26461 "2019-07-17T12:57:51Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [July 17, 2019, 12:57pm UTC](https://discourse.julialang.org/t/global-variables-anonymous-function/26461/1 "2019-07-17T12:57:51Z")

</div>

We should avoid global variables, such as, for example:

```julia
a = 2
function f(x)
    return (x + a)^2 
end
f(3)

```

As I understand, one way to pass data into a function is the use of anonymous functions. In this case,  
we could do:

```julia
a = 2
function f(x,g)
  return g(x)^2
end
f(3, x -> x+a)

```

Also I am not sure if this is equivalent in the function call:

```julia
g(x) = x + a 
f(3,g)

```

First, as I understand, this second syntax results in faster code. If this is correct, I wander if this is equivalent:

```julia
a = 2
g(x) = x + a
function f(x)
  return g(x)^2
end
f(3)

```

The variable `a`, in this last example, is a global variable being used in `g(x)`, so it seems that the problem of the first code is back.

Am I thinking this correctly? Is the anonymous function option effectively different in implementation and, thus, results faster code?

Or else we should always pass `a` as an argument explicitly to `f`, as in

```julia
a = 2
function f(x,a)
  return (x + a)^2
end
f(3)

```

Thank you for your considerations.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 17, 2019, 1:02pm UTC](https://discourse.julialang.org/t/global-variables-anonymous-function/26461/2 "2019-07-17T13:02:36Z")

</div>

> [@lmiq](#):
>
> As I understand, one way to pass data into a function is the use of implicit functions. In this case, we could do:

`a` is still global here and you will still have bad performance.

> [@lmiq](#):
>
> Or else we should always pass `a` as an argument explicitly to `f` , as in

Yes, or use `const a = ...`

---

<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:** [July 17, 2019, 1:09pm UTC](https://discourse.julialang.org/t/global-variables-anonymous-function/26461/3 "2019-07-17T13:09:42Z")

</div>

Ok, so in a typical scenario in which, for example, a minimizer calls a function in a specific format,  
for instance

```julia
function minimize(f)
   # do whatever here, but calls f with the syntax f(x) and I don't want to change that
end

```

If I want to implement an `f` which uses several parameters I probably should do:

```julia
g(x,a) = x + a
a = 2
f(x) = g(x,a)
minimize(f)

```

or

```julia
const a = 2
f(x) = x + a
minimize(f)

```

Wright?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 17, 2019, 1:14pm UTC](https://discourse.julialang.org/t/global-variables-anonymous-function/26461/4 "2019-07-17T13:14:55Z")

</div>

Or something like

```julia
g(x,a) = x + a
function run_stuff()
    a = 2
    minimize(x -> g(x, a)
end

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 17, 2019, 1:53pm UTC](https://discourse.julialang.org/t/global-variables-anonymous-function/26461/5 "2019-07-17T13:53:59Z")

</div>

Usually I would do something like

```julia
objective(model, a, x) = ...

degrees_of_freedom(model) = ...

function parametric_minimum(model, a; x0 = zeros(degrees_of_freedom(model))
    opt = minimizer(x -> objective(model, a, x), x0)
    if !is_converged(opt)
        error("could not find minimum")
    end
    get_minimum(opt)
end

```
