# Scope of variables inside functions, for loops, and if statments

**URL:** <https://discourse.julialang.org/t/scope-of-variables-inside-functions-for-loops-and-if-statments/38981>\
**Category:** General Usage\
**Created:** [May 7, 2020, 7:10pm UTC](https://discourse.julialang.org/t/scope-of-variables-inside-functions-for-loops-and-if-statments/38981 "2020-05-07T19:10:06Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 7, 2020, 9:26pm UTC](https://discourse.julialang.org/t/scope-of-variables-inside-functions-for-loops-and-if-statments/38981/5 "2020-05-07T21:26:49Z")

</div>

> [@asb](#):
>
> I interpreted the julia documentation on scope to mean that declaring a variable as “global” in a loop extends its scope to the context in which the “for” loop is embedded, so in this case it would be local to the function.

Declaring something global makes it global, not local to the function. You can either use a `let` like @Vasily_Pisarev did or put `local am, dt` at the top of the function body to declare those variables in the outermost scope of the function instead of the loop body where they are assigned.

Please quote your code: [Please read: make it easier to help you - #11](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/11)

---

_[View the full topic](https://discourse.julialang.org/t/scope-of-variables-inside-functions-for-loops-and-if-statments/38981)._
