Difference between global and const

#1

At the moment I do not understand the difference between global and const used at the global scale. I understand that there can be global variables that change while const variables do not, and that I should not use global variables, but then const is defined as used to declare global variables whose values will not change. In almost all code (and particularly performance sensitive code) global variables should be declared constant in this way.

Does this mean I should try to avoid all const in the global scope? Would that not make more work for Julia if I am reusing the same constants within different functions?

How to change or redefine a (global?) variable from within a while loop
#2

No, just the opposite. If you have a global variable, you can and should mark it as const.

If a global variable is not const, then its type could change at any time, and therefore the compiler cannot generate efficient specialized code when you use that variable.

3 Likes
#3

Thank you for the clarification!