# How do I pass a function-like object as argument to a PeriodicCallback in DifferentialEquations?

**URL:** <https://discourse.julialang.org/t/how-do-i-pass-a-function-like-object-as-argument-to-a-periodiccallback-in-differentialequations/27535>\
**Category:** New to Julia\
**Tags:** diffeq\
**Created:** [August 14, 2019, 2:08pm UTC](https://discourse.julialang.org/t/how-do-i-pass-a-function-like-object-as-argument-to-a-periodiccallback-in-differentialequations/27535 "2019-08-14T14:08:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [August 14, 2019, 2:08pm UTC](https://discourse.julialang.org/t/how-do-i-pass-a-function-like-object-as-argument-to-a-periodiccallback-in-differentialequations/27535/1 "2019-08-14T14:08:53Z")

</div>

[EDIT: I can no longer recreate my original error and can confirm that function-like objects can be used as callback functions to pass to solve in DifferentialEquations.jl.]

If I create a struct and a function-like object (as described in [https://pkg.julialang.org/docs/julia/THl1k/1.1.1/manual/methods.html#Function-like-objects-1](https://pkg.julialang.org/docs/julia/THl1k/1.1.1/manual/methods.html#Function-like-objects-1)), how can I get a handle on the function as opposed to the object itself? I want to pass one as a function to PeriodicCallback in solve (using DifferentialEquations), but I get a MethodError: objects of type Controller are not callable (Controller is a struct I’ve defined).

If I create a named function rather than using the function-like object technique, it works as expected.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [August 15, 2019, 9:58am UTC](https://discourse.julialang.org/t/how-do-i-pass-a-function-like-object-as-argument-to-a-periodiccallback-in-differentialequations/27535/2 "2019-08-15T09:58:56Z")

</div>

Does calling your object work in the REPL?

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [August 15, 2019, 1:14pm UTC](https://discourse.julialang.org/t/how-do-i-pass-a-function-like-object-as-argument-to-a-periodiccallback-in-differentialequations/27535/3 "2019-08-15T13:14:06Z")

</div>

Hmm, I’m going to say yes, as I cannot now recreate my original issue.

I had worked around my issue by creating a normal named function of signature

```julia
function fctrl (controller, integrator)
   # some control stuff here
end

```

and then making an instance of my Controller and using function composition to get a function with a single argument acceptable as a callback.

```julia
controller = Controller( ... )
f(integrator) = fctrl(controller, integrator)

```

and passing f as the callback worked.

This morning I went back to the function-like object construction to recreate the error, so now things look like

```julia
controller = Controller( ... )
function (c::Controller)(integrator)
   # some control stuff here
end

```

and just passing controller as the callback function is now working. At this point I have to assume I did something else wrong in my previous construction of the function-like object that led to the errors I was getting.
