Defining a variable within a function at the module scope?


#1

Hi,

I was wondering how can i define a variable at the module scope within a function or code block.
Is my only option “global” keyword?
or is there function define(MODULE,sym)

Thanks


#2

Why do you need another option, and what is wrong with using global?


#3

i am just asking…


#4

You could use eval, as that always operates in global scope, but that is even “worse” than global.


#5

It would be better if you explained what you are trying to accomplish.

You should never define a global variable within a function, only access it. That’s what global is for. In practice, if you want to provide type information to the compiler, you would use a container anyway, so you may not even need that. For example,

module Foo

const v = [1.0]

function change(Δ)
    v[1] += Δ
end

end