The scope of a declared element is the set of all code that can get access to it directly, without a qualified name or made available by Require. An element can have scope at one of these positions:
- Block scope
- Available only in the block of code in which it is declared. This is the smallest scope available.
- Procedure scope
- Available to all code in the procedure in which it is declared.
- Module scope
- Available to all code in the module, class, or trait in which it is declared.
- Namespace scope
- Available to all code in the namespace in which it is declared. This is the largest scope available.
You specify the scope of an element when you declare it. The scope depends on these factors:
- The position at which you declare the element – block, procedure, etc.
- The access level of the element.
Be careful when defining variables with the same name in different scopes, as the results can be unexpected. See References to Declared Elements for more information.
A block is a set of statements between an initial statement clause and an end clause. Some examples of blocks follow.
If you declare a variable in a block, you can use that variable only in that block.
Choice of scope
Advantages of local variables