According to the rules of lexical scope, any variables defined in the surrounding scope are accessible in all other scopes it contains.
In Figure 1 above, any variable defined in the top level scope will be accessible in the module’s scope and any scope nested inside it, and so on.
There is an exception to this rule which applies only to named functions: any variable coming from the surrounding scope has to be unquoted inside a function clause body.
defmodule M do
a = 1
# 'a' inside unquote() unambiguously refers to 'a' defined
# in the module's scope
def a, do: unquote(a)
# 'a' inside the body unambiguously refers to the function 'a/0'
def a(b), do: a + b
end
M.a #=> 1
M.a 3 #=> 4