Scope modifiers: Difference between revisions

m
{{out}}
No edit summary
m ({{out}})
Line 1:
{{task|Basic language learning}}
{{task|Basic language learning}}Most programming languages offer support for [[Creating a function|subroutines]]. When execution changes between subroutines, different sets of variables and functions ("scopes") are available to the program. Frequently these sets are defined by the placement of the variable and function declarations ("static scoping" or "lexical scoping"). These sets may also be defined by special modifiers to the variable and function declarations.
Most programming languages offer support for [[Creating a function|subroutines]].
When execution changes between subroutines, different sets of variables and functions ("scopes") are available to the program.
Frequently these sets are defined by the placement of the variable and function declarations ("static scoping" or "lexical scoping").
These sets may also be defined by special modifiers to the variable and function declarations.
 
Show the different scope modifiers available in your language and briefly explain how they change the scope of their variable or function. If your language has no scope modifiers, note it.
If your language has no scope modifiers, note it.
 
=={{header|Ada}}==
===Public and private declarative parts===
In [[Ada]] declarative region of a package has publicly visible and private parts. The private part is introduced by '''private''':
The private part is introduced by '''private''':
<lang ada>package P is
... -- Declarations placed here are publicly visible
Line 74 ⟶ 80:
40 PRINT FN F(2)
50 PRINT FN G(3)</lang>
{{out}}
'''Output:'''
<pre>2
1
Line 119 ⟶ 125:
ENDPROC</lang>
{{out}}
'''Output:'''
<pre>
Before function call:
Line 745 ⟶ 751:
 
=={{header|PicoLisp}}==
PicoLisp distinguishes between "scope" and "binding". The scope of a symbol
The scope of a symbol determines its visibility in a given context (whether or not it can be
(whether or not it can be accessed), while binding is about assigning it a value.
 
===Scope===
 
In PicoLisp, the scope type of a symbol is either "internal", "transient" or
"external".
"external". It is specified lexically: Internal symbols are just normal symbols.
Transient symbols are surrounded by double quotes (and thus look like strings in
other languages), and/or with an underlined font if possible. External symbols
External symbols are surrounded by braces.
 
* The scope of an internal symbol is global. This means that a symbol like AB123 is always the same object, residing at a certain location in memory (pointer equality).
Line 825 ⟶ 832:
CloseConsole()
EndIf</lang>
{{out}}
Code output:
<pre>Bob and Susan are 30 and 35 years old.
Amy and Susan are 10 and 18 years old.</pre>
Line 1,066 ⟶ 1,073:
nsB::showOff varInB
nsB::showOff localVar</lang>
{{out}}
Output:
<pre>variable globalVar holds "This is a global variable"
variable varInA holds "This is a variable in nsA"
Line 1,084 ⟶ 1,091:
}
}
[example new] showOff</lang>Output:<pre>variable objVar holds "This is an object variable"</pre>
{{out}}
<pre>variable objVar holds "This is an object variable"</pre>
 
===Commands===
Anonymous user