Julia 1.0 UndefVarError - Scope of Variable

后端 未结 2 1791
轮回少年
轮回少年 2021-01-14 19:54

I am moving from Julia 0.7 to 1.0. It seems that Julia\'s rule for the scope of variables changed from 0.7 to 1.0. For example, I want to run a simple loop like this:

2条回答
  •  广开言路
    2021-01-14 20:10

    Just remember that inside a function you won't use global, since the scope rules inside a function are as you would expect:

    function testscope()
        num = 0
        for i = 1:5
            if i == 3
                num = num + 1
            end
        end
        return num
    end
    
    
    julia> t = testscope()
    1
    

    The unexpected behaviour is only in REPL. More on this here

提交回复
热议问题