# Global access from within module function

**URL:** <https://discourse.julialang.org/t/global-access-from-within-module-function/103092>\
**Category:** New to Julia\
**Created:** [August 22, 2023, 6:32pm UTC](https://discourse.julialang.org/t/global-access-from-within-module-function/103092 "2023-08-22T18:32:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leibniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leibniz/32/217959_2.png) [@Leibniz](https://discourse.julialang.org/u/Leibniz)\
**Post date:** [August 22, 2023, 6:32pm UTC](https://discourse.julialang.org/t/global-access-from-within-module-function/103092/1 "2023-08-22T18:32:09Z")

</div>

Hi guys

why does the following not work and how can one fix it?

```julia
module MyModule

    x = 5
    
    function func(x)
        return (global x) + x
    end

end

using .MyModule
MyModule.func(3)

```

It gives `syntax: misplaced "global" declaration `

The `global` can be used to overwrite the global variable `x`, but read-only access is not possible?

Cheers!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 22, 2023, 6:41pm UTC](https://discourse.julialang.org/t/global-access-from-within-module-function/103092/2 "2023-08-22T18:41:01Z")

</div>

That’s just a syntax issue. You could do this:

```julia
julia> module MyModule

           x = 5
           
           function func(x)
               global x
               return x + x
           end

       end

```

(but don’t do it 😛 , as you’ll see, the result of `func(3)` is `10`, and that’s a mess)

---

<div class="post-metadata">

**Author:** ![Leibniz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leibniz/32/217959_2.png) [@Leibniz](https://discourse.julialang.org/u/Leibniz)\
**Post date:** [August 22, 2023, 9:58pm UTC](https://discourse.julialang.org/t/global-access-from-within-module-function/103092/3 "2023-08-22T21:58:56Z")

</div>

> [@lmiq](#):
>
> ```julia
> module MyModule
> 
> x = 5
>            
> function func(x)
> global x
> return x + x
> end
> 
> end
> 
> ```

So one cannot use both the global and the local x and would have to rename one of them?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 22, 2023, 10:18pm UTC](https://discourse.julialang.org/t/global-access-from-within-module-function/103092/4 "2023-08-22T22:18:08Z")

</div>

To be sincere, using the same name would be so prone to problems that I never tried that. But I guess there is way to do It.

---

<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 22, 2023, 10:40pm UTC](https://discourse.julialang.org/t/global-access-from-within-module-function/103092/5 "2023-08-22T22:40:29Z")

</div>

> [@Leibniz](#):
>
> So one cannot use both the global and the local x and would have to rename one of them?

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-auto
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-auto
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)

```
