# Variable scope/function usage

**URL:** <https://discourse.julialang.org/t/variable-scope-function-usage/45226>\
**Category:** General Usage\
**Tags:** question, scope, function\
**Created:** [August 19, 2020, 7:20pm UTC](https://discourse.julialang.org/t/variable-scope-function-usage/45226 "2020-08-19T19:20:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)\
**Post date:** [August 19, 2020, 7:20pm UTC](https://discourse.julialang.org/t/variable-scope-function-usage/45226/1 "2020-08-19T19:20:57Z")

</div>

A variable declared outside a function is recognized inside a function without actually passing it as an argument to it. For e.g. the script below runs perfectly.

```julia
const y = 3
function operate(x)
    println(x^2+y)
end
operate(5)

```

Is it recommended to use global variables this way without actually passing them to the function? I would prefer to do something like this instead because of better readability:

```julia
const y = 3
function operate(x,z)
    println(x^2+z)
end
operate(5,y)

```

Which way is more preferred? I understand that for this MWE there is no performance difference, but does the two approaches lead to any significant performance differences for _large_ & _complicated_ functions?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 19, 2020, 7:45pm UTC](https://discourse.julialang.org/t/variable-scope-function-usage/45226/2 "2020-08-19T19:45:28Z")

</div>

> [@trathi](#):
>
> Is it recommended to use global variables this way without actually passing them to the function?

Nope 🙂.

The very first [performance tip](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables-1) recommends against it, and it’s generally considered to be bad style to rely on global variables in any programming language, since they make the flow of information in your code harder to understand.

Independent of performance issues, the manual says:

> Passing arguments to functions is better style. It leads to more reusable code and clarifies what the inputs and outputs are.

and I am inclined to agree.

---

<div class="post-metadata">

**Author:** ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)\
**Post date:** [August 19, 2020, 7:54pm UTC](https://discourse.julialang.org/t/variable-scope-function-usage/45226/3 "2020-08-19T19:54:05Z")

</div>

Got it! Thank you 👍
