# Modify variable in closure and type inference

**URL:** <https://discourse.julialang.org/t/modify-variable-in-closure-and-type-inference/24765>\
**Category:** General Usage\
**Tags:** closure\
**Created:** [May 30, 2019, 12:55pm UTC](https://discourse.julialang.org/t/modify-variable-in-closure-and-type-inference/24765 "2019-05-30T12:55:44Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 30, 2019, 4:41pm UTC](https://discourse.julialang.org/t/modify-variable-in-closure-and-type-inference/24765/2 "2019-05-30T16:41:49Z")

</div>

You can use a `Ref`, like this:

```julia
julia> function mysum(f, itr)
         sum = Ref(0.0)
         fancy_iteration(itr) do pt
           sum[] += f(pt)
         end
         sum[]
       end
mysum (generic function with 1 method)

```

---

_[View the full topic](https://discourse.julialang.org/t/modify-variable-in-closure-and-type-inference/24765)._
