# How to return additional arguments when optimizing or finding a root

**URL:** https://discourse.julialang.org/t/how-to-return-additional-arguments-when-optimizing-or-finding-a-root/117994
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [August 9, 2024, 11:16am UTC](https://discourse.julialang.org/t/how-to-return-additional-arguments-when-optimizing-or-finding-a-root/117994 "2024-08-09T11:16:54Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)
#### Post date: [August 9, 2024, 11:16am UTC](https://discourse.julialang.org/t/how-to-return-additional-arguments-when-optimizing-or-finding-a-root/117994/1 "2024-08-09T11:16:54Z")

</div>

Hi All,  
I am looking for some ideas about how others have often handled this issue. Often, I have to solve an optimization problem and then ~~pass out~~ return some additional arguments. In the MWE example below, y is the component to minimized. But I would also like to make `a, b, c` available outside of the function. How have you folks typically handled this.

```julia
using Optim
function test(x)
    x = x[]
    a = 1
    b = 2
    c = 3
    y = x^2 - 4
    #return y^2, a, b, c
    return y^2
end

optimize(x -> test(x), [0.5]).minimizer

Edit: was using "pass out arguments" which turned out to be confusing, now using the more accurate "return".

```

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [August 9, 2024, 12:37pm UTC](https://discourse.julialang.org/t/how-to-return-additional-arguments-when-optimizing-or-finding-a-root/117994/2 "2024-08-09T12:37:01Z")

</div>

If your are passing an anonymous function already, why not use:

```julia
using Optim
function test(x)
    x = x[]
    a = 1
    b = 2
    c = 3
    y = x^2 - 4
    return y^2, a, b, c
    #return y^2
end

optimize(x -> test(x)[1], [0.5]).minimizer

```

Also note that `x -> foo(x)` can always just be written as `foo`.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 9, 2024, 12:57pm UTC](https://discourse.julialang.org/t/how-to-return-additional-arguments-when-optimizing-or-finding-a-root/117994/3 "2024-08-09T12:57:45Z")

</div>

> [@jovansam](#):
>
> But I would also like to make `a, b, c` available outside of the function. How have you folks typically handled this? […] `optimize(x -> test(x), [0.5]).minimizer`

If you have a function `test(x,a,b,c)`, then just do:

```julia
optimize(x -> test(x,a,b,c), [0.5]).minimizer

```

(This is “capturing” the values `a,b,c` in the [closure](https://en.wikipedia.org/wiki/Closure_(computer_programming)) `x -> ...` via lexical scoping.)

Or maybe I’m misunderstanding you. Do you want to pass `a,b,c` _into_ `test`, or do you want to return them _out_ of `test` (e.g. at the location of the optimum `x`)? As @abraemer says, one option for the latter is just to return additional values and then discard them during optimization; then, after optimization, you can call your function again with the optimum `x` and get the additional return values.

---

<div class="post-metadata">

### Author: ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)
#### Post date: [August 9, 2024, 3:09pm UTC](https://discourse.julialang.org/t/how-to-return-additional-arguments-when-optimizing-or-finding-a-root/117994/4 "2024-08-09T15:09:29Z")

</div>

Thanks @stevengj. Using “pass out” was indeed confusing. I meant  
return.

---

<div class="post-metadata">

### Author: ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)
#### Post date: [August 9, 2024, 3:22pm UTC](https://discourse.julialang.org/t/how-to-return-additional-arguments-when-optimizing-or-finding-a-root/117994/5 "2024-08-09T15:22:07Z")

</div>

Thanks this works too! For posterity here is an approach I have used previously.

```julia
using Optim
function test(x; run = "solve")
    x = x[]
    a = 1
    b = 2
    c = 3
    y = x^2 - 4
    return run == "solve" ? y^2 : (y^2, a, b, c)
end

# Finding the solution run.
x = optimize(x -> test(x), [0.5]).minimizer

# the simulation run to return additional arguments. note that this simply MWE.
test(x; run = "simulate")

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 9, 2024, 4:05pm UTC](https://discourse.julialang.org/t/how-to-return-additional-arguments-when-optimizing-or-finding-a-root/117994/6 "2024-08-09T16:05:08Z")

</div>

> [@jovansam](#):
>
> `return run == "solve" ? y^2 : (y^2, a, b, c)`

I would recommend against this because [it is type-unstable](https://docs.julialang.org/en/v1/manual/performance-tips/#Write-%22type-stable%22-functions) and will slow things down considerably. Just returning extra values and discarding them at the call site should be efficient.

---

<div class="post-metadata">

### Author: ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)
#### Post date: [August 9, 2024, 4:12pm UTC](https://discourse.julialang.org/t/how-to-return-additional-arguments-when-optimizing-or-finding-a-root/117994/7 "2024-08-09T16:12:02Z")

</div>

Thanks for the tip!
