# How to avoid using global variable in "for loop" inside a function?

**URL:** <https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288>\
**Category:** General Usage\
**Tags:** question, scope, functions\
**Created:** [September 16, 2021, 10:00pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288 "2021-09-16T22:00:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 10:00pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288/1 "2021-09-16T22:00:33Z")

</div>

I have two questions please:  
1- Are `r, s` global or local variables in the below `main code`?

```julia
function ss(r, s)
u =r+s;
a =r*s;
return u, a
end

#main code
r=5;
s=9;
q,w = ss(r, s)

```

2- How to get ride of defining `iter` as a global variable inside `for loop` in the `pp` function?

```julia
function pp()
iter=0;
for i in 1:10
global iter +=1;
end
end

```

---

<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:** [September 16, 2021, 10:08pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288/2 "2021-09-16T22:08:02Z")

</div>

1. When you pass them as parameters, they will be in the scope of the function (always do that). That is: they are global in the “main code”, but when you pass them as parameters to the function they are local to the function. The function will specialize for their type, and all is good there.

2. In this case it should work without the global. If you do not want to initialize it, use `local iter`:

> [@Amro](#):
>
> ```julia
> function pp()
> iter = 0 # or `local iter` if you do not want to initialize it
> for i in 1:10
> iter +=1
> end
> end
> 
> ```

i. e. something like:

```julia
function pp()
    local iter
    for i in 1:10
        iter =i
    end
    return iter
end

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 10:17pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288/3 "2021-09-16T22:17:11Z")

</div>

> [@lmiq](#):
>
> When you pass them as parameters, they will be in the scope of the function (always do that). That is: they are global in the “main code”, but when you pass them as parameters to the function they are local to the function. The function will specialize for their type, and all is good there.

So in the blow code, `r, s` are being passed as parameters, aren’t they!

```julia
function ss(r, s)
u =r+s;
a =r*s;
return u, a
end

#main code
r=5;
s=9;
q,w = ss(r, s)

```

Actually I want to initialize because the iteration range is different. and Sorry I was asking if there is no function, i.e., I just want to run the below code

```julia
    iter = 0
    for i in 1:10
        global iter +=1
    end

```

---

<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:** [September 16, 2021, 10:20pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288/4 "2021-09-16T22:20:48Z")

</div>

> [@Amro](#):
>
> So in the blow code, `r, s` are being passed as parameters, aren’t they!

Yes, that code is perfectly fine. You have created `r` and `s` in the global scope, but you have passed them as parameters to the function. That is how things should be done.

> [@Amro](#):
>
> Actually I want to initialize because the iteration range is different:

Perfect, that is how it is done. (you can also do `for i in 0:9`, of course, if you are not using that counter for anything else).

---

<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:** [September 16, 2021, 10:22pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288/5 "2021-09-16T22:22:18Z")

</div>

> [@Amro](#):
>
> ```julia
> iter = 0
> for i in 1:10
> global iter +=1
> end
> 
> ```

You should avoid that if you want any performance. The scoping rules in that case are a little bit tricky. Read this: [Scope of loops · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/loopscopes/)

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 10:25pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288/6 "2021-09-16T22:25:10Z")

</div>

> [@lmiq](#):
>
> You should avoid that if you want any performance. The scoping rules in that case are a little bit tricky. Read this: [Scope of loops · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/loopscopes/)

This is because I have a main code which I put at its early beginning all the modules and packages. This code has the main `for loop` which has that `iter` variable which I need to get ride off being global.

---

<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:** [September 16, 2021, 10:28pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288/8 "2021-09-16T22:28:10Z")

</div>

The variable will be global if you are in the global scope. What you mean exactly?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 10:30pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288/9 "2021-09-16T22:30:59Z")

</div>

So, the global scope is the same as in the main code, right? I mean since `iter` is in the main code(global scope), then defining `iter` as global in the `for loop` won’t change anything in the performance, right?

```julia
#main code
#dowork()
iter = 0
    for i in 1:10
        global iter +=1
    end

```

---

<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:** [September 16, 2021, 10:38pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288/10 "2021-09-16T22:38:29Z")

</div>

> [@Amro](#):
>
> `iter` as global in the `for loop` won’t change anything in the performance, right?

You are forced to declare it global (unless you type that in the REPL), otherwise you will get an error.

Performance will suffer, as iter is a global non-constant variable.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [September 16, 2021, 10:40pm UTC](https://discourse.julialang.org/t/how-to-avoid-using-global-variable-in-for-loop-inside-a-function/68288/11 "2021-09-16T22:40:37Z")

</div>

> [@lmiq](#):
>
> You are forced to declare it global (unless you type that in the REPL), otherwise you will get an error.
> 
> Performance will suffer, as iter is a global non-constant variable.

Thank you very much for your explanations!
