I installed JuliaPro 1.4.1-1 this morning and opened up some beginner Julia tutorials.
The code below runs perfectly in a Jupyter Notebook, but gives an UndefVarError when I run it in Juno, whether I run it in the REPL or as a .jl file.
n = 0
while n < 4
n += 1
println(n)
end
When I run it as a .jl, the error message reads:
Error evaluating firsttest.jl
LoadError: UndefVarError: n not defined
in expression starting at C:\Users\Lipkin Hollow\JuliaPro-1.4.1-1\firsttest.jl:2
top-level scope at firsttest.jl:3
When I run it in REPL, the error reads:
ERROR: UndefVarError: n not defined
Stacktrace:
[1] top-level scope at .\none:3
Any help would be appreciated!
You've run into current scoping rules in Julia: loops introduce their own scope, so you can't access a global variable from within the loop. The relevant documentation is here
IJulia currently uses a package called SoftGlobalScope.jl to change this scoping behaviour, as it turned out to be one of the more controversial changes introduced in Julia version 1.0. That's why behaviour is currently different between IJulia/Jupyter Notebooks and other settings (i.e. REPL and Juno).
Note that this behaviour will change again in Julia version 1.5, which will bring the behaviour at the REPL in line with what you now see in Jupyter. The announcement for this can be found here.
In general though, the use of global variables in Julia is strongly discouraged for performance reasons, and you should look to organise your code in functions to make the most of Julia!