Correct. Variable scope declarations actually happen across the whole scope at once, they don’t run in order like operations and calls. Hence this:
julia> x = 5
5
julia> function func(x) # local x
result = x+x # uses global x
global x
return result
end
func (generic function with 1 method)
julia> func(3)
10
The error is because global x is a variable declaration (can be left side of an assignment), not an access of a value (can be right side of assignment).
julia> function getx()
global x # only left side, lack of value
end
ERROR: syntax: misplaced "global" declaration
Stacktrace:
[1] top-level scope
@ REPL[17]:1
julia> function getx()
global x = 5 # 5 on right side as value
end
getx (generic function with 1 method)
julia> function getx()
global x
x # not a declaration, just accesses the value
end
getx (generic function with 1 method)