# How to give information into a callback function

**URL:** <https://discourse.julialang.org/t/how-to-give-information-into-a-callback-function/12805>\
**Category:** Optimization (Mathematical)\
**Created:** [August 1, 2018, 11:36pm UTC](https://discourse.julialang.org/t/how-to-give-information-into-a-callback-function/12805 "2018-08-01T23:36:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CDRPico](https://avatars.discourse-cdn.com/v4/letter/c/2bfe46/32.png) [@CDRPico](https://discourse.julialang.org/u/CDRPico)\
**Post date:** [August 1, 2018, 11:36pm UTC](https://discourse.julialang.org/t/how-to-give-information-into-a-callback-function/12805/1 "2018-08-01T23:36:38Z")

</div>

Hi everyone.

I’m implementing a callback function for lazy constraints (Benders decomposition). I’m not sure if it is possible to give and recover information to the callback. The idea is to pass a global variable which can change its value according to some criteria into the function. So far, It seems like the cb function is not recognizing global variables, declared previously, when I try to use it inside (i.e. the interesting one). I got “ERROR: UndefRefError: access to undefined reference”. Furthermore, when the variable’s value is modified, I don’t know how to recover it. The value of that variable is important at each time when cb function is called.

Is there any way to do that?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [August 2, 2018, 1:05am UTC](https://discourse.julialang.org/t/how-to-give-information-into-a-callback-function/12805/2 "2018-08-02T01:05:07Z")

</div>

Hi @CDRPico, please post a [minimum working example](https://stackoverflow.com/help/mcve) that demonstrates what you are trying to do and the error you are getting.

You may also be interested in this [Benders decomponsition example](https://github.com/JuliaOpt/juliaopt-notebooks/blob/3110eddaf5effdecfee687739295bea05731ba33/notebooks/Shuvomoy%20-%20Benders%20decomposition.ipynb).

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [August 2, 2018, 6:40am UTC](https://discourse.julialang.org/t/how-to-give-information-into-a-callback-function/12805/3 "2018-08-02T06:40:50Z")

</div>

If the callback needs to have a state, maybe make it a callable struct? Silly Example:

```julia
mutable struct Foo
    a::int;
end

function (foo::Foo)(x)
    r = x+foo.a
    foo.a = x
    return r
end

```

Running it:

```julia
# constructing the "callback"
julia> f=Foo(1)
Foo(1)

# calling it
julia> f(3)
4

# its state has changed
julia> f
Foo(3)

```
