# Variable declaration without assignment

**URL:** <https://discourse.julialang.org/t/variable-declaration-without-assignment/102548>\
**Category:** New to Julia\
**Created:** [August 7, 2023, 7:05am UTC](https://discourse.julialang.org/t/variable-declaration-without-assignment/102548 "2023-08-07T07:05:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![michakraus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michakraus/32/222105_2.png) [@michakraus](https://discourse.julialang.org/u/michakraus)\
**Post date:** [August 7, 2023, 7:05am UTC](https://discourse.julialang.org/t/variable-declaration-without-assignment/102548/1 "2023-08-07T07:05:04Z")

</div>

Hi, I have a question about what happens internally, when variables are declared without actually assigning a value, e.g., if I have a function like

```julia
function myfunc()
    local x
end

```

does this actually create a variable or does it just tell the compiler “Should a variable x be accessed within this scope, it should be a local variable” ?

After all, if I try to access this variable, e.g., by

```julia
function myfunc()
    local x
    x
end
myfunc()

```

I obtain an `UndefVarError`, which indicates that the variable has not actually been declared.  
The fact that the actual position of `local x` in the method does not play a role seems to support this thesis, i.e., that the following code still throws an error:

```julia
x = 2
function myfunc()
    x
    local x
end
myfunc()

```

Maybe I am lacking some understanding here. I would appreciate any clarifications that help understanding what is actually happening under the hood.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 7, 2023, 11:28am UTC](https://discourse.julialang.org/t/variable-declaration-without-assignment/102548/2 "2023-08-07T11:28:39Z")

</div>

> [@michakraus](#):
>
> I obtain an `UndefVarError`, which indicates that the variable has not actually been declared.

It’s declared, yet it’s undefined. It just means it wasn’t assigned an instance yet.

```julia-auto
julia> global x2
julia> hasproperty(@ __MODULE__ , :x2)
true
julia> isdefined(@ __MODULE__ , :x2)
false
julia> getproperty(@ __MODULE__ , :x2)
ERROR: UndefVarError: x2 not defined

```

> [@michakraus](#):
>
> ```julia-auto
> x = 2
> function myfunc()
> x
> local x
> end
> 
> ```

Declarations apply to the whole scope, they’re not something that’s evaluated by line order. That first `x` is thus local, and evaluating it throws an error because there’s no instance.

---

<div class="post-metadata">

**Author:** ![michakraus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michakraus/32/222105_2.png) [@michakraus](https://discourse.julialang.org/u/michakraus)\
**Post date:** [August 7, 2023, 1:56pm UTC](https://discourse.julialang.org/t/variable-declaration-without-assignment/102548/3 "2023-08-07T13:56:58Z")

</div>

Many thanks for the quickly reply. That clarifies everything!
