# Global variables / performance / data passing

**URL:** <https://discourse.julialang.org/t/global-variables-performance-data-passing/19069>\
**Category:** New to Julia\
**Created:** [December 28, 2018, 6:25pm UTC](https://discourse.julialang.org/t/global-variables-performance-data-passing/19069 "2018-12-28T18:25:49Z")\
**Posts on this page:** 2\
**Page:** 2

<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:** [January 1, 2019, 10:08pm UTC](https://discourse.julialang.org/t/global-variables-performance-data-passing/19069/21 "2019-01-01T22:08:59Z")

</div>

So, this code structure would be fine?:

```julia
struct Data
  a :: Int64
end

const data = Data(4)

# Function that computes the function value

function computef(x :: Float64, data :: Data)
  f = data.a * x^2
  return f
end

# New function with different call, appropriate for "minimize" below

computef(x :: Float64) = computef(x,data)

# This the routine that requires a "computef(x)" function (dummy example)

function minimize(x)
  fcurrent = computef(x)
  y = x - 0.1
  fnew = computef(y)
  if ( fnew < fcurrent )
    return y, fnew
  else
    return x, fcurrent
  end
end

# Main program

x = 10.
println(" x = ",x," f = ",computef(x,data))
x, f = minimize(x)
println(" x = ",x," f = ",computef(x,data))

```

At the same time, I understand that most Julia functions would require the function “computef” as a parameter, and this structure would be more natural:

```julia
struct Data
  a :: Int64
end

const data = Data(4)

# Function that computes the function value

function computef(x :: Float64, data :: Data)
  f = data.a * x^2
  return f
end

# This the routine that requires a "computef(x)" function (dummy example)

function minimize(x,computef)
  fcurrent = computef(x)
  y = x - 0.1
  fnew = computef(y)
  if ( fnew < fcurrent )
    return y, fnew
  else
    return x, fcurrent
  end
end

# Main program

x = 10.
println(" x = ",x," f = ", computef(x,data))

# Passing the function as an argument, as a closure
x, f = minimize(x, x -> computef(x,data) )

println(" x = ",x," f = ", computef(x,data))

```

Did I get things right? Is there any performance advice that should be added here?

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [January 2, 2019, 5:33am UTC](https://discourse.julialang.org/t/global-variables-performance-data-passing/19069/22 "2019-01-02T05:33:03Z")

</div>

That looks good to me (the second version since, like you point out, you don’t want to hardcode the function to optimize).

One small suggestion: It is common in Julia to put the function argument first, like this:

```julia
function minimize(computef, x)

```

That will enable the “[do-block-syntax](https://docs.julialang.org/en/v1/manual/functions/index.html#Do-Block-Syntax-for-Function-Arguments-1)”; a convenient and readable way to create anonymous functions:

```julia
x, f = minimize(x) do y
    z = zero(y)
    for n = 1:data.a
      z += y * n
    end
    z
end

```

[Previous page](https://discourse.julialang.org/t/global-variables-performance-data-passing/19069.md?page=1)
