# How to make a function store data to avoid repeating computation

**URL:** <https://discourse.julialang.org/t/how-to-make-a-function-store-data-to-avoid-repeating-computation/88396>\
**Category:** General Usage\
**Tags:** question, functions, style, memoize\
**Created:** [October 7, 2022, 11:45am UTC](https://discourse.julialang.org/t/how-to-make-a-function-store-data-to-avoid-repeating-computation/88396 "2022-10-07T11:45:50Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![ArthurW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arthurw/32/19561_2.png) [@ArthurW](https://discourse.julialang.org/u/ArthurW)\
**Post date:** [October 7, 2022, 4:36pm UTC](https://discourse.julialang.org/t/how-to-make-a-function-store-data-to-avoid-repeating-computation/88396/6 "2022-10-07T16:36:47Z")

</div>

You could redefine `f`. It looks a bit better and can be seen as a “Julia thing”.

```julia
julia> function f()
           global _output = (println("heavy calculations"); 12)
           @eval f() = _output
           return _output
       end
f (generic function with 1 method)

julia> f()
heavy calculations
12

julia> f()
12

```

More generally, it seems you want to emulate the behaviour of local static variables from C/C++. There are multiple requests for such feature (see [this issue](https://github.com/JuliaLang/julia/issues/15056)).

---

_[View the full topic](https://discourse.julialang.org/t/how-to-make-a-function-store-data-to-avoid-repeating-computation/88396)._
