Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Scope (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Part of a computer program where a given name binding is valid}} {{more citations needed|date=December 2008}} In [[computer programming]], the '''scope''' of a [[name binding]] (an association of a name to an entity, such as a [[Variable (programming)|variable]]) is the part of a [[Computer program|program]] where the name binding is valid; that is, where the name can be used to refer to the [[entity]]. In other parts of the program, the name may refer to a different entity (it may have a different binding), or to nothing at all (it may be unbound). Scope helps prevent [[name collision]]s by allowing the same name to refer to different objects – as long as the names have separate scopes. The scope of a name binding is also known as the '''visibility''' of an entity, particularly in older or more technical literature—this is in relation to the referenced entity, not the referencing name. The term "scope" is also used to refer to the set of ''all'' name bindings that are valid within a part of a program or at a given point in a program, which is more correctly referred to as ''context'' or ''environment''.{{efn|1=See [[#Definition|definition]] for meaning of "scope" versus "context".}} Strictly speaking{{efn|"Dynamic scope" bases name resolution on ''extent'' (lifetime), not ''scope'', and thus is formally inaccurate.}} and in practice for most [[programming language]]s, "part of a program" refers to a portion of [[source code]] (area of text), and is known as '''lexical scope'''. In some languages, however, "part of a program" refers to a portion of [[Runtime (program lifecycle phase)|run time]] (period during [[Execution (computing)|execution]]), and is known as '''dynamic scope'''. Both of these terms are somewhat misleading—they misuse technical terms, as discussed in the [[#Definition|definition]]—but the distinction itself is accurate and precise, and these are the standard respective terms. Lexical scope is the main focus of this article, with dynamic scope understood by contrast with lexical scope. In most cases, name resolution based on lexical scope is relatively straightforward to use and to implement, as in use one can read backwards in the source code to determine to which entity a name refers, and in [[Programming language implementation|implementation]] one can maintain a list of names and contexts when [[compiling]] or [[Interpreter (computing)|interpreting]] a program. Difficulties arise in [[name masking]], [[forward declaration]]s, and [[variable hoisting|hoisting]], while considerably subtler ones arise with [[non-local variable]]s, particularly in [[Closure (computer programming)|closure]]s.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)