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!
== Definition == The strict definition of the (lexical) "scope" of a name ([[identifier]]) is unambiguous: lexical scope is "the portion of source code in which a binding of a name with an entity applies". This is virtually unchanged from its 1960 definition in the specification of [[ALGOL 60]]. Representative language specifications follow: ; ALGOL 60 (1960)<ref name="RALA60 2.7">"Report on the Algorithmic Language Algol 60", 2.7. Quantities, kinds and scopes</ref> : The following kinds of quantities are distinguished: simple variables, arrays, labels, switches, and procedures. The scope of a quantity is the set of statements and expressions in which the declaration of the identifier associated with that quantity is valid. ; [[C (programming language)|C]] (2007)<ref>[http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf WG14 N1256] (2007 updated version of the [[C99]] standard), 6.2.1 Scopes of identifiers, 2007-09-07</ref> : An identifier can denote an object; a function; a tag or a member of a structure, union, or enumeration; a [[typedef]] name; a label name; a macro name; or a macro parameter. The same identifier can denote different entities at different points in the program. [...] For each different entity that an identifier designates, the identifier is ''visible'' (i.e., can be used) only within a region of program text called its ''scope.'' ; [[Go (programming language)|Go]] (2013)<ref name=go>[http://golang.org/ref/spec The Go Programming Language Specification]: [http://golang.org/ref/spec#Declarations_and_scope Declarations and scope], Version of Nov 13, 2013</ref> : A declaration binds a non-blank identifier to a constant, type, variable, function, label, or package. [...] The scope of a declared identifier is the extent of source text in which the identifier denotes the specified constant, type, variable, function, label, or package. Most commonly "scope" refers to when a given name can refer to a given [[Variable (programming)|variable]]—when a [[Declaration (computer programming)|declaration]] has effect—but can also apply to other entities, such as functions, types, classes, [[Label (computer science)|labels]], constants, and enumerations. === Lexical scope vs. dynamic scope === A fundamental distinction in scope is what "part of a program" means. In languages with '''lexical scope''' (also called '''static scope'''), name resolution depends on the location in the source code and the ''lexical context'' (also called ''static context''), which is defined by where the named variable or function is defined. In contrast, in languages with '''dynamic scope,''' the name resolution depends upon the [[program state]] when the name is encountered which is determined by the ''execution context'' (also called ''runtime context'', ''calling context'' or ''dynamic context''). In practice, with lexical scope a name is resolved by searching the local lexical context, then if that fails, by searching the outer lexical context, and so on; whereas with dynamic scope, a name is resolved by searching the local execution context, then if that fails, by searching the outer execution context, and so on, progressing up the call stack.<ref name=Borning /> Most modern languages use lexical scope for variables and functions, though dynamic scope is used in some languages, notably some dialects of Lisp, some "scripting" languages, and some [[template language]]s.{{efn|1=For example, the [[Jinja (template engine)|Jinja]] template engine for Python by default uses both lexical scope (for imports) and dynamic scope (for includes), and allows behavior to be specified with keywords; see [http://jinja.pocoo.org/docs/templates/#import-context-behavior Import Context Behavior].}} Perl 5 offers both lexical and dynamic scope. Even in lexically scoped languages, scope for [[Closure (computer science)|closures]] can be confusing to the uninitiated,{{citation needed|date=March 2022}} as these depend on the lexical context where the closure is defined, not where it is called. Lexical resolution can be determined at [[compile time]], and is also known as ''early binding'', while dynamic resolution can in general only be determined at [[Run time (program lifecycle phase)|run time]], and thus is known as ''late binding''. === Related concepts === In [[object-oriented programming]], [[dynamic dispatch]] selects an object [[Method (computer programming)|method]] at runtime, though whether the actual name binding is done at compile time or run time depends on the language. De facto dynamic scope is common in [[Macro (computer science)|macro languages]], which do not directly do name resolution, but instead expand in place. Some programming frameworks like [[AngularJS#Scope|AngularJS]] use the term "scope" to mean something entirely different than how it is used in this article. In those frameworks, the scope is just an object of the programming language that they use ([[JavaScript]] in case of AngularJS) that is used in certain ways by the framework to emulate dynamic scope in a language that uses lexical scope for its variables. Those [[AngularJS#Scope|AngularJS scopes]] can themselves be in context or not in context (using the usual meaning of the term) in any given part of the program, following the usual rules of variable scope of the language like any other object, and using their own [[Inheritance (object-oriented programming)|inheritance]] and [[transclusion]] rules. In the context of AngularJS, sometimes the term "$scope" (with a dollar sign) is used to avoid confusion, but using the dollar sign in variable names is often discouraged by the style guides.<ref name="js-conventions">{{cite web|url=https://docs.angularjs.org/guide/scope|title=Code Conventions for the JavaScript Programming Language|first1=Douglas|last1=Crockford|access-date=2015-01-04}}</ref>
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)