Each block in a method creates a declaration space for local variables. Names are introduced into this declaration space through local variable declarations in the method body and the formal parameter list of the method, which introduces names into the outermost block's declaration space. Blocks do not allow shadowing of names through nesting – once a name has been declared in a block, the name may not redeclared in any nested blocks.
A method invocation creates a copy, specific to that invocation, of the local variables of the method. A local variable comes into existence when control enters the method body that contains the local variable declaration and ceases to exist when control leaves the method. All locals are initialized to their type's default value. Local variables are always publicly accessible. It is an error to refer to the local variable in a textual position that precedes its declaration.
When the method is a function, a special local is implicitly declared in the method body's declaration space with the same name as the method representing the return value of the function. The local has special semantics when used in expressions: if the local is used in a non-variable expression, then the name will bind to the function rather than to the local. For example, given a function F
, the expression F = 1
references the function return local, while x = F(1)
is a recursive call to F
. The use of parenthesis can cause ambiguous situations (such as F(1)
where F
is a function whose return type is a one-dimensional array); in all ambiguous situations, the name binds to the function rather than the local. When control flow leaves the method body, the value of the local is passed back to the invocation expression. If the method is a subroutine, there is no such implicit local and control simply returns to the invocation expression.
If explicit local declaration is specified by the compilation environment or Option
Explicit
, then all local variables must be explicitly declared. Otherwise, local variables may be implicitly declared simply by using them in a statement, with the first textual appearance of the identifier considered its "declaration." The one exception to this rule is that local variables may not be explicitly declared in function invocation or indexing expressions. Implicitly declared locals are always scoped to the entire method body. In the case of implicit declaration, the type of the variable is Object
if no type character was attached to the implicit declaration; otherwise the type of the variable is the type of the type character.