Running this code
module mod1
export foo
function foo()
i=0
for ii in 1:10
global i+=ii
end
i
end
end
mod1.foo()
gives UndefVarError: i not defined
.
It seems that the way to do it is by adding a global
before the variable i
:
module mod2
export bar
function bar()
global i=0
for ii in 1:10
global i+=ii
end
i
end
end
mod2.bar()
This gives: 55
Why does the first method not work? As I understand the for
introduces a new scope. Therefore I need the global inside the loop. But why do I also need it outside of the loop?
(I am using julia 1.5.0)