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!
=== Function scope === When the scope of variables declared within a function does not extend beyond that function, this is known as '''function scope'''.<ref>{{cite web |title=Functions - Javascript:MDN |date=23 April 2023 |url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#function_scope |quote=Variables defined inside a function cannot be accessed from anywhere outside the function, because the variable is defined only in the scope of the function. However, a function can access all variables and functions defined inside the scope in which it is defined.}}</ref> Function scope is available in most programming languages which offer a way to create a ''[[local variable]]'' in a function or [[subroutine]]: a variable whose scope ends (that goes out of context) when the function returns. In most cases the lifetime of the variable is the duration of the function call—it is an [[automatic variable]], created when the function starts (or the variable is declared), destroyed when the function returns—while the scope of the variable is within the function, though the meaning of "within" depends on whether scope is lexical or dynamic. However, some languages, such as C, also provide for [[static local variable]]s, where the lifetime of the variable is the entire lifetime of the program, but the variable is only in context when inside the function. In the case of static local variables, the variable is created when the program initializes, and destroyed only when the program terminates, as with a [[static global variable]], but is only in context within a function, like an automatic local variable. Importantly, in lexical scope a variable with function scope has scope only within the ''lexical context'' of the function: it goes out of context when another function is called within the function, and comes back into context when the function returns—called functions have no access to the local variables of calling functions, and local variables are only in context within the body of the function in which they are declared. By contrast, in dynamic scope, the scope extends to the ''execution context'' of the function: local variables ''stay in context'' when another function is called, only going out of context when the defining function ends, and thus local variables are in context of the function in which they are defined ''and all called functions''. In languages with lexical scope and [[nested function]]s, local variables are in context for nested functions, since these are within the same lexical context, but not for other functions that are not lexically nested. A local variable of an enclosing function is known as a [[non-local variable]] for the nested function. Function scope is also applicable to [[anonymous function]]s. <syntaxhighlight lang="python" style="float:right;margin-left:1em"> def square(n): return n * n def sum_of_squares(n): total = 0 i = 0 while i <= n: total += square(i) i += 1 return total </syntaxhighlight> For example, in the snippet of Python code on the right, two functions are defined: <code>square</code> and <code>sum_of_squares</code>. <code>square</code> computes the square of a number; <code>sum_of_squares</code> computes the sum of all squares up to a number. (For example, <code>square(4)</code> is 4<sup>2</sup> = <code>16</code>, and <code>sum_of_squares(4)</code> is 0<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 3<sup>2</sup> + 4<sup>2</sup> = <code>30</code>.) Each of these functions has a variable named <var>n</var> that represents the argument to the function. These two <var>n</var> variables are completely separate and unrelated, despite having the same name, because they are lexically scoped local variables with function scope: each one's scope is its own, lexically separate function and thus, they don't overlap. Therefore, <code>sum_of_squares</code> can call <code>square</code> without its own <var>n</var> being altered. Similarly, <code>sum_of_squares</code> has variables named <var>total</var> and <var>i</var>; these variables, because of their limited scope, will not interfere with any variables named <var>total</var> or <var>i</var> that might belong to any other function. In other words, there is no risk of a ''name collision'' between these names and any unrelated names, even if they are identical. No name masking is occurring: only one variable named <var>n</var> is in context at any given time, as the scopes do not overlap. By contrast, were a similar fragment to be written in a language with dynamic scope, the <var>n</var> in the calling function would remain in context in the called function—the scopes would overlap—and would be masked ("shadowed") by the new <var>n</var> in the called function. Function scope is significantly more complicated if functions are first-class objects and can be created locally to a function and then returned. In this case any variables in the nested function that are not local to it (unbound variables in the function definition, that resolve to variables in an enclosing context) create a [[Closure (computer science)|closure]], as not only the function itself, but also its context (of variables) must be returned, and then potentially called in a different context. This requires significantly more support from the compiler, and can complicate program analysis.
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)