# How to obtain local variables in functions for nlsolve or minimize

**URL:** <https://discourse.julialang.org/t/how-to-obtain-local-variables-in-functions-for-nlsolve-or-minimize/32437>\
**Category:** New to Julia\
**Tags:** question, optimization\
**Created:** [December 18, 2019, 11:20am UTC](https://discourse.julialang.org/t/how-to-obtain-local-variables-in-functions-for-nlsolve-or-minimize/32437 "2019-12-18T11:20:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![RangeFu](https://avatars.discourse-cdn.com/v4/letter/r/e9c0ed/32.png) [@RangeFu](https://discourse.julialang.org/u/RangeFu)\
**Post date:** [December 18, 2019, 11:20am UTC](https://discourse.julialang.org/t/how-to-obtain-local-variables-in-functions-for-nlsolve-or-minimize/32437/1 "2019-12-18T11:20:12Z")

</div>

Hi,  
I was wondering what is the best practice to export local variables in a objective function for optimization or linear solvers. For example, I have a function that like:

```julia
function example(x)
    b = sum(x)
    return b * x' * x

```

then I send this function to a minimizer. Assume it converges. I would like to obtain the value of b in the last iteration. How can I achieve that?

I have several tentative solutions in mind:

- globalize b with const affix (wrap it with an array container).
- Add a flag argument to indicate whether to output b, then run the function again with the indicator.
- Take a mutable container of b as an argument and modify it as the side effect of the function.

My understanding is, global variables have performance issues so I should try to avoid; the second approach is sure-fire, but it requires additional iteration, which doesn’t sound very efficient (though in most of my application the cost is low enough). I’m currently employing the third method and it works well so far. However, as I’m new to Julia, I’m not sure whether it has some hidden issues (e.g., compatibility with other packages like ForwardDiff?).

Thanks for any comment!

---

<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:** [December 19, 2019, 2:53pm UTC](https://discourse.julialang.org/t/how-to-obtain-local-variables-in-functions-for-nlsolve-or-minimize/32437/2 "2019-12-19T14:53:00Z")

</div>

I would recommend coding it as

```julia
calculate_interim_values(x) = (xx = x' * x, b = sum(x))

calculate_objective(v) = v.xx * v.b

```

and then optimize the `calculate_objective ∘ calculate_interim_values`, and then call the latter on the optimal position.

This _does_ require an extra function call, but for most optimization problems that’s not a big deal.

An ideal solution would be optimizer libraries accepting a return value that can carry some kind of extra payload, with the objective being available using an accessor, eg

```julia
get_value(x::Real) = x

```

with the user redefining the latter for arbitrary objects. But this does require a change in the optimization library.
