# Closure without parameter

**URL:** <https://discourse.julialang.org/t/closure-without-parameter/5213>\
**Category:** New to Julia\
**Created:** [August 3, 2017, 9:11pm UTC](https://discourse.julialang.org/t/closure-without-parameter/5213 "2017-08-03T21:11:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [August 3, 2017, 9:11pm UTC](https://discourse.julialang.org/t/closure-without-parameter/5213/1 "2017-08-03T21:11:56Z")

</div>

Hi,

I need to provide a function to a c++ library and I want the user to pass a Julia function. The c++ function is like `f(dy,t,y)` and is called all over the place in the c++ code. I have succeeded in the past to write closures when the function definition is `f(dy,t,y,param)` by copying the method used in [Sundials](https://github.com/JuliaDiffEq/Sundials.jl).

However I have no clue how to deal with this situation.

Does anyone have an idea?

Thank you very much,

Best regards.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 3, 2017, 9:32pm UTC](https://discourse.julialang.org/t/closure-without-parameter/5213/2 "2017-08-03T21:32:31Z")

</div>

No you currently can’t do it

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [August 4, 2017, 7:05am UTC](https://discourse.julialang.org/t/closure-without-parameter/5213/3 "2017-08-04T07:05:26Z")

</div>

You mean that no trick is known?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 4, 2017, 7:40am UTC](https://discourse.julialang.org/t/closure-without-parameter/5213/4 "2017-08-04T07:40:56Z")

</div>

You mean like [the ParameterizedFunction constructor](http://docs.juliadiffeq.org/latest/analysis/parameterized_functions.html#ParameterizedFunction-Constructor-1) that encloses parameters `f(t,u,p,du)` into `f(t,u,du)`? That’s done with a call-overloaded type and is used with Sundials all the time.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [August 4, 2017, 7:53am UTC](https://discourse.julialang.org/t/closure-without-parameter/5213/5 "2017-08-04T07:53:43Z")

</div>

I think this is made possible in Sundials because the C code asks for a function `rhs(t,u,p,du)` where you can pass a parameter to the function `rhs`, and thus you pass a pointer to your julia function `rhs_julia`. Do I understand fully your remark @ChrisRackauckas ?

Basically, I need a closure to insert a wrapping part (see below) in the code (provided by the user) the code

```julia
function rhs_passed_to_c(Dy,t,y)
  # wrapping part
  Dy_ = unsafe_wrap(Array,Dy,N)
  y_ = ConstArray(y,N)
  # user provided code
  rhs_julia(Dy_,t,y_)
  nothing
end

```

I guess a macro would do but I was wondering about other solutions.
