# Function, constants, globals

**URL:** <https://discourse.julialang.org/t/function-constants-globals/48744>\
**Category:** General Usage\
**Tags:** question, constants\
**Created:** [October 21, 2020, 12:39pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744 "2020-10-21T12:39:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![neo\_abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neo_abs/32/16672_2.png) [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Post date:** [October 21, 2020, 12:39pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/1 "2020-10-21T12:39:37Z")

</div>

I wanted to have a function:

```julia
function test()
    const global x = 1
end

```

Obviously it does not work. Is there a workaround that would make it feasible to declare constant globals from function?

I want to have some sort of ‘function’ that I would later save with JLD2 so I could reload the parameters later on. If there’s some other way I could do it let me know.  
Or is there some other way?

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [October 21, 2020, 12:43pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/2 "2020-10-21T12:43:49Z")

</div>

Not sure I get what you want, but maybe you can do something like

```julia
function make_constant()
   ... # assign the value you need to `c`
   return c
end

const c = make_constant()

```

---

<div class="post-metadata">

**Author:** ![neo\_abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neo_abs/32/16672_2.png) [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Post date:** [October 21, 2020, 12:50pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/3 "2020-10-21T12:50:37Z")

</div>

In my code it looks like that:

```julia
function test()
   "Definition of x1"
    const global x1 = 1
   "Definition of x2"
    const global x2 = 1
# many more
   "Definition of xk_1"
    const global xk_1 = 1
   "Definition of xk"
    const global xk = 1

end

```

What you propose is quite inefficient from perspective of a user

---

<div class="post-metadata">

**Author:** ![neo\_abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neo_abs/32/16672_2.png) [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Post date:** [October 21, 2020, 12:51pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/4 "2020-10-21T12:51:30Z")

</div>

Right now I have a script that defines constants. I just want to remake it into something ‘portable’

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 21, 2020, 1:17pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/5 "2020-10-21T13:17:36Z")

</div>

The way you should do this is with a NamedTuple or struct (mutable or otherwise) or, if you really need the ability to add or remove constants, a global constant Dict.

---

<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:** [October 21, 2020, 2:06pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/6 "2020-10-21T14:06:48Z")

</div>

From your description, it seems you would want something like:

```julia
julia> using Parameters

julia> @with_kw struct MyConsts
          x = 1
          y = 2
       end
MyConsts

julia> const myconsts = MyConsts()
MyConsts
  x: Int64 1
  y: Int64 2

julia> myconsts.x
1

```

Yet, probably if you explain better what exactly do you want to achieve, you can get better alternatives.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 21, 2020, 9:03pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/7 "2020-10-21T21:03:57Z")

</div>

Sorry for my naif question to the forum. What is wrong with saving a large quantity of constants in a separate \*.jl file?

```julia
# mycontants.jl file
const e = 2.7182818284
const γ = 0.5772156649
# etc...

```

and then collect them via code inclusion:

```julia
include("myconstants.jl");
julia> e
2.7182818284
julia> γ
0.5772156649

```

---

<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:** [October 21, 2020, 9:08pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/8 "2020-10-21T21:08:39Z")

</div>

Well, it depends, why are you not `using` a `Module` with these global variables inside?

If you just want to use include this file in the REPL there is no problem.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [October 21, 2020, 9:21pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/9 "2020-10-21T21:21:30Z")

</div>

Functions are not meant to modify global scopes, but macros can:

```julia
macro makeglobal()
    quote
        const global x=1
    end
end

```

And then you call `@makeglobal`. (Side note: you can also define the macro in another module; it gets evaluated in the caller’s scope.)

I think this is closer to the OP’s request, although less tidy than other suggestions already given.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 21, 2020, 9:40pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/10 "2020-10-21T21:40:11Z")

</div>

TLDR: if your constants aren’t really constant, don’t tell julia they are.

* * *

> [@heliosdrm](#):
>
> Functions are not meant to modify global scopes, but macros can:

> [@heliosdrm](#):
>
> I think this is closer to the OP’s request, although less tidy than other suggestions already given.

It is not only less tidy, it is also not nearly as flexible, and can also be dangerous if it isn’t understood properly. People sometimes assume you can just do :

```julia
const a = 10
# define some functions that "capture" `a` and run them
# later I want to change a and rerun my code, so I run:
a = 11
# rerun my functions from before

```

But you can’t. If the value of `a` was captured in any functions that have now been compiled, changing `a` without also redefining those functions will lead to the wrong result. This is why the following warning is issued at the REPL when you do this:

```julia
WARNING: redefinition of constant a. This may fail, cause incorrect answers, or produce other errors.

```

Basically, global constants should be treated as global _constants_, and the fact that julia lets you change them (as long as their type doesn’t change) can be misleading if you don’t understand that.

So in that case, if you take care to follow the rules (which is already a hassle), you’re subjecting yourself to a lot of unnecessary redefinition and recompilation. So if the constants _aren’t literally meant to be constant_, put them into something mutable or temporary.

You will see this same advice issued pretty often on here, as this is a common question 🙂

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 21, 2020, 9:46pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/11 "2020-10-21T21:46:33Z")

</div>

There’s nothing wrong with this at all! It could be convenient to wrap these in a module, but there is nothing problematic about doing exactly this in your own code.

The OP sounded like the constants will change. I.e.

> [@neo\_abs](#):
>
> I want to have some sort of ‘function’ that I would later save with JLD2 so I could reload the parameters later on. If there’s some other way I could do it let me know.

sounds like different parameters could be saved and loaded (otherwise presumably they would stick to their current setup, and it sounded like they were outgrowing it.)

By the way, math constants are available through `Base.MathConstants`, in case you didn’t already know:

```julia
julia> Base.MathConstants.γ
γ = 0.5772156649015...

```

---

<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:** [October 21, 2020, 10:04pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/12 "2020-10-21T22:04:55Z")

</div>

> [@tomerarnon](#):
>
> wrap these in a module

Perhaps we can help showing exactly what this means:

```julia
julia> module Const
          a = 3.
          b = 4.
          export a, b
       end
Main.Const

julia> using .Const # be aware of the dot

julia> a
3.0

julia> b
4.0

julia> a = 3
ERROR: cannot assign a value to variable Const.a from module Main
Stacktrace:
 [1] top-level scope at REPL[7]:1

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 21, 2020, 11:16pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/13 "2020-10-21T23:16:35Z")

</div>

For this simple case of defining global constants, could someone explain for Julia beginners like me, the benefits of including a separate file containing a module wrapping all the constants, versus simple code inclusion? Thank you.

---

<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:** [October 21, 2020, 11:44pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/14 "2020-10-21T23:44:37Z")

</div>

Not many, but one advantage would be that you can optionally choose not to import all constants:

```julia
julia> module Const
          a = 3.
          b = 4.
          export a, b
       end
Main.Const

julia> using .Const: a 

julia> a
3.0

julia> b
ERROR: UndefVarError: b not defined

```

But I think we should warn that if a code depends on the definition of a lot constants, probably they should be included in a struct or other data structure and passed as parameters to the functions that will use them.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [October 22, 2020, 8:10am UTC](https://discourse.julialang.org/t/function-constants-globals/48744/15 "2020-10-22T08:10:46Z")

</div>

In this case, you could just do:

```julia
function test()
    @eval const x = 1
end

```

---

<div class="post-metadata">

**Author:** ![neo\_abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neo_abs/32/16672_2.png) [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Post date:** [October 23, 2020, 6:07pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/16 "2020-10-23T18:07:31Z")

</div>

That’s what I needed, thank you!

In the end my code looks like that:

```julia
function test()

  @eval begin

      "Definition of x1"
       const global x1 = 1
      "Definition of x2"
       const global x2 = 1
      # many more
      "Definition of xk_1"
       const global xk_1 = 1
      "Definition of xk"
       const global xk = 1
   
   end

end

```

---

<div class="post-metadata">

**Author:** ![neo\_abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neo_abs/32/16672_2.png) [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Post date:** [October 23, 2020, 6:12pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/17 "2020-10-23T18:12:28Z")

</div>

My constants do not change.  
I have a solver for some class of problems. Part of each problem are parameters. They repeat in many parts of problem definition. Thus I wanted to make use of constants to speed up calculations. Solved problem is saved with `JLD2.jl` so I could access it later (in different workspace) but until now I could not restore parameters as they were stored in a script.  
If I changed parameters (changed problem) old setting was gone. I need function literally for a purpose of saving it.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [October 23, 2020, 6:27pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/18 "2020-10-23T18:27:52Z")

</div>

(You do not need the `global` here, since `eval` always evaluates in global scope)

---

<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:** [October 23, 2020, 6:40pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/19 "2020-10-23T18:40:32Z")

</div>

This is a typical scenario. The most portable solutions involve storing the parameters in a struct and/or passing the model function as a parameter to the solver using closures, while the model function receives as input parameters the constants in a data structure.

Having the parameters of each model in a struct also allows very easy saving of the model data, for example using JSON.

---

<div class="post-metadata">

**Author:** ![neo\_abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neo_abs/32/16672_2.png) [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Post date:** [October 23, 2020, 6:53pm UTC](https://discourse.julialang.org/t/function-constants-globals/48744/20 "2020-10-23T18:53:35Z")

</div>

It’s not empirical model but theoretical.  
Won’t const be faster than NaamedTuple?

[Next page](https://discourse.julialang.org/t/function-constants-globals/48744.md?page=2)
