ERROR: UndefVarError: A not defined

g(x) = 2 + x
A = 0

for i =1:2
A += g(i)
end

I got an error: ERROR: UndefVarError: A not defined.
I thought that I defined A = 0 before the loop. How to fix this?
Thank you in advanced!

I found out that I need to add global

g(x) = 2 + x
A = 0

for i =1:2 
           global A += g(i)
end

Or

g(x) = 2 + x
function f()
A = 0
for i =1:2 
  A += g(i)
end
return A
end
1 Like

thank you. this occur when I upgrade Julia version. A bit confusing …

From pre 1.0 (or 0.7), right? (Remember to use 0.7 to get deprecation warnings during upgrade)

1 Like