# How to calculate a constant the first time a function is called?

**URL:** <https://discourse.julialang.org/t/how-to-calculate-a-constant-the-first-time-a-function-is-called/77506>\
**Category:** General Usage\
**Created:** [March 7, 2022, 1:41pm UTC](https://discourse.julialang.org/t/how-to-calculate-a-constant-the-first-time-a-function-is-called/77506 "2022-03-07T13:41:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [March 7, 2022, 1:41pm UTC](https://discourse.julialang.org/t/how-to-calculate-a-constant-the-first-time-a-function-is-called/77506/1 "2022-03-07T13:41:00Z")

</div>

Hello, how to calculate a constant the first time a function is called?  
not when compiling  
not every call

```julia
foo() = (sleep(1); 1) # foo calculate a constant
bar(x) = foo() + x # ????

# first call:
@time bar(2) # -> 1.001082 seconds (5 allocations: 144 bytes)
# second call:
@time bar(2) # -> 0.000001 seconds

```

---

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [March 7, 2022, 1:52pm UTC](https://discourse.julialang.org/t/how-to-calculate-a-constant-the-first-time-a-function-is-called/77506/2 "2022-03-07T13:52:21Z")

</div>

may be, i dont know  
but looks like \*hit

```julia
global FOO = nothing #

foo() = (sleep(1); global FOO = 1) # foo calculate a constant

bar(x) = try
    FOO + x
catch
    foo()
    @eval bar(x) = $FOO + x
    return FOO + x
end

@time bar(22) # -> 1.001544 seconds (247 allocations: 12.344 KiB)
@time bar(22) # -> 0.000001 seconds

```

---

<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:** [March 7, 2022, 1:55pm UTC](https://discourse.julialang.org/t/how-to-calculate-a-constant-the-first-time-a-function-is-called/77506/3 "2022-03-07T13:55:19Z")

</div>

see this thread: [In Julia, how to create a function that saves its own internal state?](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457)

---

<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:** [March 7, 2022, 2:06pm UTC](https://discourse.julialang.org/t/how-to-calculate-a-constant-the-first-time-a-function-is-called/77506/5 "2022-03-07T14:06:48Z")

</div>

Here’s a pretty simple way to do it:

```julia
const FOO = Ref{Union{Int, Nothing}}(nothing)

function foo()
  if FOO[] === nothing
    println("expensive calculation running...")
    sleep(1)
    FOO[] = 1
  end
  FOO[]
end

bar(x) = foo() + x

```

Demo:

```julia
julia> bar(1)
expensive calculation running...
2

julia> bar(2)
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:** [March 7, 2022, 2:16pm UTC](https://discourse.julialang.org/t/how-to-calculate-a-constant-the-first-time-a-function-is-called/77506/6 "2022-03-07T14:16:09Z")

</div>

Or, if you do not trust union splitting, you can have one global with a boolean to indicate if the function was already called or not; and the global with the Ref starting with a dummy/undef value.

---

<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:** [March 7, 2022, 2:27pm UTC](https://discourse.julialang.org/t/how-to-calculate-a-constant-the-first-time-a-function-is-called/77506/7 "2022-03-07T14:27:05Z")

</div>

If your function has some input, you might better avoid at all this global state, and wrap the constant precomputed in the input, such that the function is completely self-contained. Something like:

```julia
julia> struct MyInput
           x::Vector{Float64}
           constant::Float64
       end

julia> MyInput(x::Vector{Float64}) = MyInput(x, length(x)*sum(x)) # invented a calculation for the constant
MyInput

julia> input = MyInput(rand(3)) # here the constant is computed
MyInput([0.607776859483833, 0.630463715553019, 0.01606684134650216], 3.762922249150063)

julia> function foo!(input)
           (;x, constant) = input
           x .= constant .* x # mutates the `x` entry of input
       end
foo (generic function with 1 method)

julia> foo!(input)
MyInput([2.2870170670702663, 2.372385942536272, 0.06045827477631713], 3.762922249150063)

julia> foo!(input)
MyInput([8.605867405864627, 8.92710384674058, 0.22749978730103176], 3.762922249150063)

```
