4.1 Basic local variable

This is a simple example that does not really do much, other than illustrating the nature of local variables.


Listing 5: Example to illustrate how local variables are created
1define sub s1 
2  local x 
3  xy 
4  y x+1 
5end define sub 
6y
7invoke s1






traceA BC D

explanation







line#y

“y” is a global variable, it exists as soon as the program starts to execute, and it only ceases to exit when the program ends.







pre ?







6
2







7
retline# x

When we allocate a column for “retline#”, we also allocate a column for each local variable of the subroutine being invoked.







post ?

A local variable starts with an unknown value.







3
2

There are two important points here. First, the global variable “y” is accessible in the subroutine. Second, a local variable can start values.







4
3







5
retline# x

At the end of a subroutine, when the “retline#” column is deallocated, all local variables should be deallocated as well. This means that the local variable “x” is not only invisible after this point, it actually does not even exist!







post