Variable addition (scope issue)


#1

I have the following code, and it is not working. Why is this?

s = 0
for i = 1:2
    s += i/2
end

#2

use global s


#3

You haven’t read the scope docs!

s = 0
for i = 1:2
    global s += i/2
end

Note that this is a controversial new feature of 1.0: https://github.com/JuliaLang/julia/issues/28789


#4

Many thanks.


#5

You should be able to mark a post as “Solved”, so people don’t waste time looking at it.