TYL PROGRAMMING LANGUAGE
▶ VARIABLE SCOPE
Variable scope is the section of the programmatic component that contains its declaration statement, starting from the declaration statement, and up to the component's block end.
In this one token program:
num
num is declared in the program block, or module in Tyl jargon, and its scope will span all areas of the program that can see it, which is essentialy all program code.

For example:
num
num 10
print num
inc numparam:
num numparam + 1
num <= 10 ?
  print 'continue'
  \
  print num
^
num scope is all the module, so all variables named 'num' are the same variable.

A variable that is declared in the module statements section is called a global variable.

Variable scope starts in the first apearance of it in the non compound statments of a code block:
print num
num 1
print num
NULL
1
In the first line print num, num will be declared as a variable. The line after it num 1, will be an assignment line, that assigns the value 1 to the already declared num.

Scoping in conditional statement:
\t ?
num 2
 \t ?
  num 4
  \
  num 5
^
 \
num 3
^
print num
NULL
print num prints NULL because it is the the first apearance of it a non compound statment in the containing block. All other statements with variables named 'num' are inside the conditional block tree, and their scope is fully contained in the block tree.

Also num variables of statements 'num 2' and 'num 3', are not in the same scope. But num 4, is a child block of the block that contains num 2, and therefore num 4 refers to num 2.

Scoping depends on block functionality:
i 10
print 'i before = ' + i
i 3 ~ print 'loop i = ' + i
print 'i after = ' + i
i before = 10
loop i = 0
loop i = 1
loop i = 2
i after = 10
line 'i 3 ~', constructs a looping mechanism that loops three times. i in this mechanism is a declared variable of it, and overrides or masks the i that was declared in the first line, thus constructs a new variable scope that will continue further down in the looping block.

So as we see in the results, i of the module is the same before and after the looping and retains its value 10, while i of the looping gets its value from the mechanism on each iteration.

Function parameter masks global variables:
num 1
pr 2
pr num: print num
2
Function parameters scope is the collection of all of the statements that are included in its block, as well as all contained blocks. Therefore, num in statement print num, refers to num parameter of pr function, and not the global num.

Note that when speaking about statements ownership, statements are not just operative sections of code, but also can or do contain another statements. Therefore, to find a variable's declaration statement, we have to search up the containment tree.

In this looping example:
loop 1
loop ind: ind 1 ~ print ind
1
ind of the looping statement 'ind 1 ~', is declared but will not be seen in the looping block because ind parameter of loop function is masking it.

Full discussion about scoping is beyond the scope of this guide, but it is important to say that each programming language has its definitions of it, and there are differences between them.

Here are some principles of scoping in Tyl, arranged by masking order: