Can't Change Vector inside loop

I’m confused with the next error:

julia> A = ones(10);

julia> A = A - 0.25.*A
10-element Array{Float64,1}:
 0.75
 0.75
 0.75
 0.75
 0.75
 0.75
 0.75
 0.75
 0.75
 0.75

julia> for i = 1:2
       A = A - 0.25.*A
       end
ERROR: UndefVarError: A not defined
Stacktrace:
 [1] top-level scope at ./REPL[3]:2 [inlined]
 [2] top-level scope at ./none:0

You need to declare A global in the loop, since it’s a global variable being modified.

If this code was in a function, this would not be necessary. This behavior is new in 0.7

3 Likes