Reputation: 83
The following code fails.
global Θ=1.0
function f(a)
c=sin(a+θ)
return c
end
f(1)
UndefVarError: θ not defined
Stacktrace:
[1] f(a::Int64)
@ Main ./In[1]:3
[2] top-level scope
@ In[1]:6
[3] eval
@ ./boot.jl:373 [inlined]
[4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
It has no reason to fails.
Why this is incorrect??
If this doesn't work, I can say that people can't do anything using Julia.
Upvotes: 1
Views: 400
Reputation: 14695
You're using two different characters: the global variable is an uppercase Theta, while the variable you're referring to inside the function is a lowercase theta.
julia> 'Θ'
'Θ': Unicode U+0398 (category Lu: Letter, uppercase)
julia> 'θ'
'θ': Unicode U+03B8 (category Ll: Letter, lowercase)
The lowercase theta θ is a new variable that's never been defined, hence the UndefVarError
.
Fixing that:
julia> Θ=1.0
1.0
julia> function f(a)
c=sin(a+Θ)
return c
end
f (generic function with 2 methods)
julia> f(1)
0.9092974268256817
Note that you don't need to mark the global variable as global
- it's global by default just by being defined outside any function or other local scope. (global
is only needed when you're in a local scope and want to assign to an existing global variable.)
Upvotes: 2