# In Julia, how to create a function that saves its own internal state?

**URL:** <https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457>\
**Category:** New to Julia\
**Tags:** question, function\
**Created:** [April 2, 2021, 5:21pm UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457 "2021-04-02T17:21:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![erwanlecarpentier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erwanlecarpentier/32/23448_2.png) [@erwanlecarpentier](https://discourse.julialang.org/u/erwanlecarpentier)\
**Post date:** [April 2, 2021, 5:21pm UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/1 "2021-04-02T17:21:33Z")

</div>

Hey!  
I would like to create a function with an internal state, but I do not want to have to initialize this internal state outside of the function (I want to rely only on calls to this function and I do not want the function to return anything). I think the following code does what I mean:

```julia
function foo()
    if @isdefined state
        global state
        state += 1
    else
        global state
        state = 1 # 1st call: initialize internal state
    end
end

```

which gives the following expected output:

```julia
julia> foo()
1
julia> foo()
2
julia> foo()
3

```

However, I see drawbacks with this code, e.g., maybe working with global mutable variable is not the best way as it is computationally expensive. On the other hand, I cannot think of a way to achieve what I described without using global variables. Note that the state of the function would only be accessed by the function itself. Any thought / advice?

---

<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:** [April 2, 2021, 5:34pm UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/2 "2021-04-02T17:34:09Z")

</div>

You can use a `let` block to hide the state:

```julia
julia> let state = Ref{Union{Int, Nothing}}(nothing)
         global f
         function f()
           if state[] !== nothing
             state[] += 1
           else
             state[] = 1
           end
           state[]
         end
       end
f (generic function with 2 methods)

julia> f()
1

julia> f()
2

julia> f()
3

```

Note that we need `global f` just to ensure that the function `f` is defined in global scope. This doesn’t cause any performance issues because functions are (implicitly) `const`, and constant globals are ok.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 2, 2021, 5:41pm UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/3 "2021-04-02T17:41:27Z")

</div>

One way to do this is by spawning a coroutine (a task) and then ask that task to give you data:

```julia
function coroutine(c::Channel)
    state = rand(100)
    for s in state
    	push!(c, s)
    end
    return
end

c = Channel(1)
t = @async coroutine(c)
take!(c)
take!(c)
# etc

```

Exactly how to structure it depends a bit on your application.

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [April 2, 2021, 5:45pm UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/4 "2021-04-02T17:45:10Z")

</div>

Use a closure:

```julia
julia> f(state=0) = ()->state+=1
f (generic function with 2 methods)

julia> foo = f()
#7 (generic function with 1 method)

julia> foo()
1

julia> foo()
2

julia> foo()
3

julia> foo.state
Core.Box(3)

julia> foo.state.contents
3

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 2, 2021, 6:02pm UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/5 "2021-04-02T18:02:42Z")

</div>

Just to complement,

> [@pbayer](#):
>
> ```julia
> julia> foo.state
> Core.Box(3)
> 
> ```

is not considered part of the language API and may change in the future, breaking code that makes use of this feature.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 2, 2021, 7:01pm UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/6 "2021-04-02T19:01:04Z")

</div>

Or a functor?

```julia
struct F
  state
end
(o::F)(x) = o.state + x

f = F(1)

f(2)
3

```

more specifically:

```julia
julia> Base.@kwdef mutable struct F
         state = nothing
       end
F

julia> function (o::F)()
         if o.state == nothing
           o.state = 1
         else
           o.state += 1
         end
       end

julia> f = F()
F(nothing)

julia> f()
1

julia> f()
2

julia> f()
3

julia> f.state # now this is part of the language, because f is an instance of F
3

```

---

<div class="post-metadata">

**Author:** ![erwanlecarpentier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erwanlecarpentier/32/23448_2.png) [@erwanlecarpentier](https://discourse.julialang.org/u/erwanlecarpentier)\
**Post date:** [April 3, 2021, 10:09am UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/7 "2021-04-03T10:09:02Z")

</div>

Thank you all!  
All those solutions work for me. After benchmarking with `@btime`, I tend to favor the `let` block as it took 5 times less execution time than the other methods.

> [@rdeits](#):
>
> ```julia
> let state = Ref{Union{Int, Nothing}}(nothing)
> global f
> function f()
> if state[] !== nothing
> state[] += 1
> else
> state[] = 1
> end
> state[]
> end
> end
> 
> ```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 3, 2021, 1:23pm UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/8 "2021-04-03T13:23:01Z")

</div>

> [@erwanlecarpentier](#):
>
> I tend to favor the `let` block as it took 5 times less execution time

I would give @lmiq’s functor approach another try with

```julia
const f = F()

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 3, 2021, 1:41pm UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/9 "2021-04-03T13:41:30Z")

</div>

And/Or declaring `state` with:

```julia
state :: Union{Int, Nothing}

```

inside the struct?

(If nothing can be a zero probably `Int` only is even better)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 3, 2021, 4:31pm UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/10 "2021-04-03T16:31:35Z")

</div>

If I benchmarked everything correctly, the functor with the pure-Int field is by far more efficient. Followed by the functor with the `Union{Nothing,Int}` field:

```julia
julia> include("state.jl")
let: 3.595 μs (0 allocations: 0 bytes)
# functor with `Any` field
f1: 29.342 μs (1000 allocations: 15.62 KiB)
f1_const: 28.184 μs (1000 allocations: 15.62 KiB)
# functor with Union field:
f2: 1.260 μs (0 allocations: 0 bytes)
f2_const: 1.259 μs (0 allocations: 0 bytes)
# functor with pure Int field
f3: 257.421 ns (0 allocations: 0 bytes)
f3_const: 257.424 ns (0 allocations: 0 bytes)

```

> **Code**
>
> ```julia
> 
> let state = Ref{Union{Int, Nothing}}(nothing)
> global f
> function f()
> if state[] !== nothing
> state[] += 1
> else
> state[] = 1
> end
> state[]
> end
> end
> 
> Base.@kwdef mutable struct F1
> state = nothing
> end
> 
> function (o::F1)()
> if o.state == nothing
> o.state = 1
> else
> o.state += 1
> end
> end
> 
> f1 = F1()
> const f1_const = F1()
> 
> Base.@kwdef mutable struct F2
> state::Union{Nothing,Int} = nothing
> end
> 
> function (o::F2)()
> if o.state == nothing
> o.state = 1
> else
> o.state += 1
> end
> end
> 
> f2 = F2()
> const f2_const = F2()
> 
> Base.@kwdef mutable struct F3
> state::Int = 0
> end
> 
> function (o::F3)()
> if o.state == 0
> o.state = 1
> else
> o.state += 1
> end
> end
> 
> f3 = F3()
> const f3_const = F3()
> 
> function update_state!(f,n) 
> for i in 1:n
> f()
> end
> end
> 
> using BenchmarkTools
> n = 1000
> print("let:"); @btime update_state!($f,$n)
> print("f1:"); @btime update_state!($f1,$n)
> print("f1_const:"); @btime update_state!($f1_const,$n)
> print("f2:"); @btime update_state!($f2,$n)
> print("f2_const:"); @btime update_state!($f2_const,$n)
> print("f3:"); @btime update_state!($f3,$n)
> print("f3_const:"); @btime update_state!($f3_const,$n)
> 
> ```

---

<div class="post-metadata">

**Author:** ![GregMcC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregmcc/32/14827_2.png) [@GregMcC](https://discourse.julialang.org/u/GregMcC)\
**Post date:** [April 7, 2021, 3:48pm UTC](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/11 "2021-04-07T15:48:37Z")

</div>

Think this is the simpliest …

> GlobalBool = [false, false , false , false, false , false , false]

then the function :-

> function IdentifyEdgeOfPress(PB :: Int , GlobalBoolPtr :: Int ) :: Int  
> bTemp = gpio\_read(PB)
> 
> if (( bTemp == true ) && ( GlobalBool[GlobalBoolPtr] == false )) ; println("Pressed Edge ") ; end  
> if (( bTemp == false ) && ( GlobalBool[GlobalBoolPtr] == true )) ; println(“Released Edge”) ; end
> 
> global GlobalBool[GlobalBoolPtr] = bTemp  
> end

then just call the function with a different index ( the indices must be not be repeated otherwise there’ll be cross contamination )

```
	IdentifyEdgeOfPress(TestPB,1)
	IdentifyEdgeOfPress(TunePB,2)

```

Bingo - you have a subroutine retaining it’s own state.  
( Thought there might be a more elegant ‘Julia’ way of doing this )
