Search code examples
matlabglobal-variablesconstantsglobalsimulink

Global CONSTANTS. Any problem using them?


I've been programming for over 6 years now and i've always avoided using global variables because there is always another way around the problems.

Today, i work on a (big) project, we want to use a dictionnary of mathematical CONSTANTS what will never be modified anywhere. The only problem i seem to find with globals on internet is the fact that if someone overwrites one it could bug out the whole project. But as mine are constants this problem doesnt apply.

(as a second security to avoid people creating a variable with the same name as one of the constants i will probably pack them all in a single global struct)

Does anyone know of problems that still happend using global constants?

Thanks for your answers! :)


Solution

  • The old-fashioned standard way to create a constant in MATLAB is to write a function. For example pi is a function. It could be written as:

    function value = pi
    value = 3.14159;
    end
    

    Of course we can overwrite the value of pi in MATLAB, but it is always a local change, it is not possible to affect another workspace.