@@ -26,7 +26,7 @@ comprehensions, broadcast-fusing                 | local | global or local
2626
2727Notably missing from this table are
2828[ begin blocks] (@ref   man-compound-expressions) and [ if blocks] (@ref   man-conditional-evaluation)
29- which do * not*  introduce new scope blocks .
29+ which do * not*  introduce new scopes .
3030Both types of scopes follow somewhat different rules which will be explained below.
3131
3232Julia uses [ lexical scoping] ( https://en.wikipedia.org/wiki/Scope_%28computer_science%29#Lexical_scoping_vs._dynamic_scoping ) ,
@@ -97,15 +97,12 @@ A new local scope is introduced by most code blocks (see above
9797[ table] (@ref   man-scope-table) for a complete list).
9898A local scope inherits all the variables from a parent local scope,
9999both for reading and writing.
100- Additionally, the local scope inherits all global variables that are assigned
101- in its parent global scope block (if it is surrounded by a global ` if `  or ` begin `  scope).
102100Unlike global scopes, local scopes are not namespaces,
103101thus variables in an inner scope cannot be retrieved from the parent scope through some sort of
104102qualified access.
105103
106104The following rules and examples pertain to local scopes.
107- A newly introduced variable in a local scope does not
108- back-propagate to its parent scope.
105+ A newly introduced variable in a local scope cannot be referenced by a parent scope.
109106For example, here the `` z ``  is not introduced into the top-level scope:
110107
111108``` jldoctest 
@@ -121,18 +118,30 @@ ERROR: UndefVarError: z not defined
121118    In this and all following examples it is assumed that their top-level is a global scope
122119    with a clean workspace, for instance a newly started REPL.
123120
124- Inside a  local scope a variable  can be forced to be a new local variable using the  [ ` local ` ] ( @ref )  keyword :
121+ Inner  local scopes  can, however, update variables in their parent scopes :
125122
126123``` jldoctest 
127- julia> x = 0; 
124+ julia> for i = 1:1 
125+            z = i 
126+            for j = 1:1 
127+                z = 0 
128+            end 
129+            println(z) 
130+        end 
131+ 0 
132+ ``` 
128133
129- julia> for i = 1:10 
130-            local x # this is also the default 
134+ Inside a local scope a variable can be forced to be a new local variable using the [ ` local ` ] ( @ref )  keyword:
135+ 
136+ ``` jldoctest 
137+ julia> for i = 1:1 
131138           x = i + 1 
139+            for j = 1:1 
140+                local x = 0 
141+            end 
142+            println(x) 
132143       end 
133- 
134- julia> x 
135- 0 
144+ 2 
136145``` 
137146
138147Inside a local scope a global variable can be assigned to by using the keyword [ ` global ` ] ( @ref ) :
@@ -163,9 +172,6 @@ julia> z
163172The ` local `  and ` global `  keywords can also be applied to destructuring assignments, e.g.
164173` local x, y = 1, 2 ` . In this case the keyword affects all listed variables.
165174
166- Local scopes are introduced by most block keywords,
167- with notable exceptions of ` begin `  and ` if ` .
168- 
169175In a local scope, all variables are inherited from its parent
170176global scope block unless:
171177
@@ -194,8 +200,8 @@ An explicit `global` is needed to assign to a global variable:
194200!!! sidebar "Avoiding globals"
195201    Avoiding changing the value of global variables is considered by many
196202    to be a programming best-practice.
197-     One reason for this is that remotely changing the state of global variables in other 
198-     modules should be done with care as it makes  the local  behavior of the  program hard  to reason about.
203+     Changing the value of a global variable can cause "action at a distance", 
204+     making  the behavior of a  program harder  to reason about.
199205    This is why the scope blocks that introduce local scope require the ` global ` 
200206    keyword to declare the intent to modify a global variable.
201207
@@ -233,9 +239,9 @@ julia> x, y # verify that global x and y are unchanged
233239(1, 2) 
234240``` 
235241
236- The reason to allow * modifying local*  variables of parent scopes in
242+ The reason to allow modifying local variables of parent scopes in
237243nested functions is to allow constructing [ ` closures ` ] ( https://en.wikipedia.org/wiki/Closure_%28computer_programming%29 ) 
238- which have a  private state, for instance the ` state `  variable in the
244+ which have private state, for instance the ` state `  variable in the
239245following example:
240246
241247``` jldoctest 
0 commit comments