In my experience, the concept of "scope" pertains exclusively to identifiers and declarations; and indeed, most of the article is only about scope insofar as it
| This is the talk page for discussing improvements to the Scope (computer programming) article. This is not a forum for general discussion of the subject of the article. |
Article policies
|
| Find sources: Google (books · news · scholar · free images · WP refs) · FENS · JSTOR · TWL |
| Archives: 1 |
| This article is rated C-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
This article may be too technical for most readers to understand. (September 2010) |
In my experience, the concept of "scope" pertains exclusively to identifiers and declarations; and indeed, most of the article is only about scope insofar as it pertains to identifiers and declarations. (Though, maybe that's not saying much: most of the article is only about variable-names, and obviously lots of other things can also have scope.) The lead text, however, seems to downplay that aspect of scope; the word "identifier" hardly appears, and instead we get text about "values and expressions", and about how scopes can "contain statements and/or expressions which define an executable algorithm or part thereof" and can "nest or be nested" (which I think are really supposed to be statements about blocks, in block-structured languages, rather than about scopes per se).
So, I think the lead text should be completely rewritten.
Before I proceed — does anyone disagree?
—RuakhTALK 04:36, 1 February 2012 (UTC)
Diego Moya (talk · contribs) removed the statement that
> Outside of its scope, a declaration or identifier effectively does not exist.
describing it as a "false claim" because "variables in C can be accessed outside their scope".
I think (s)he's mistaken, but edit-summaries aren't the place to carry out this argument, so: Diego Moya, can you explain what you mean? For example, can you give an example of valid C code where a variable-name is accessed outside of its scope?
—RuakhTALK 16:12, 23 February 2012 (UTC)
So I came here following some link trying to find out what exactly the difference between Lexical/Dynamic Scoping is. I read through the page for a few minutes and actually still hadn't much of a clue. Then I went to this page: http://c2.com/cgi/wiki?DynamicScoping
It's very easy to understand from these two paragraphs:
"In lexical scoping (and if you're the interpreter), you search in the local function (the function which is running now), then you search in the function (or scope) in which that function was defined, then you search in the function (scope) in which that function was defined, and so forth. "Lexical" here refers to text, in that you can find out what variable is being referred to by looking at the nesting of scopes in the program text.
In dynamic scoping, by contrast, you search in the local function first, then you search in the function that called the local function, then you search in the function that called that function, and so on, up the call stack. "Dynamic" refers to change, in that the call stack can be different every time a given function is called, and so the function might hit different variables depending on where it is called from."
In contrast to this the text here goes:
"In lexical scoping (or lexical scope; also called static scoping or static scope), if a variable name's scope is a certain function, then its scope is the program text of the function definition: within that text, the variable name exists, and is bound to its variable, but outside that text, the variable name does not exist. By contrast, in dynamic scoping (or dynamic scope), if a variable name's scope is a certain function, then its scope is the time-period during which the function is executing: while the function is running, the variable name exists, and is bound to its variable, but after the function returns, the variable name does not exist. "
I don't know, but this doesn't make much sense to me. I'm trying to understand what it tries to express but it isn't easy, even though I know from the above explanation what the whole thing tries to express.
--Birgitschrange (talk) 10:55, 5 January 2013 (UTC)
I was hoping to find references to the history of variable scoping in the Basic programming language. Was Microsoft Visual Basic the first to implement this feature/concept? Linhartr22 (talk) 16:51, 21 July 2013 (UTC)
A few background points about the history of variable scoping. Variable scoping or lexical scoping was first used in Algol 60 in the 1960s [I was working on computers in the late 1960s. Algol 60 was the second language I learned after Fortran.] There is I think little doubt that this is the correctly stated origin of scoping; in fact the wikipedia article on Algol 60 says so more or less. Visual Basic was introduced as a new name in the early 1990s. The original "Basic" (without Visual) dates back to the early 1960s, but had no lexical scoping. From the 1970s onwards, following the success of Algol60, a majority of general purpose programming languages used Algol-style scoping. — Preceding unsigned comment added by SamLinscho (talk • contribs) 21:36, 29 May 2021 (UTC)
File scope ends with a reference to C code that does not exist in the text. However, perhaps the author was referring to the Python code above (because of the function name)? — Preceding unsigned comment added by Polariseke (talk • contribs) 04:24, 1 December 2014 (UTC)
In this diff https://en.wikipedia.org/w/index.php?title=Scope_%28computer_science%29&type=revision&diff=762080509&oldid=753892005 I am only responsible for the change in the perl code example. The Visual Editor invented all the other changes. 93.232.43.102 (talk) 14:56, 26 January 2017 (UTC)
Fortran has BLOCK which in addition to the usual scope nesting, allows variables to be allocated. More specifically, arrays can be allocated with dimension based on values when entering the BLOCK. PL/I BEGIN blocks also do this, I believe inherited from ALGOL's BEGIN. There is no mention of Fortran, BLOCK, PL/I, and its BEGIN. As well as I know it, the lexical scope in C doesn't do allocation such that variables are still allocated at function entry. Gah4 (talk) 17:41, 31 July 2019 (UTC)
The text and the code sample for the last Perl example simply do not match. The code is a closure factory, the text describes a single function closing over one variable. IMHO, a simpler code sample would be a better match for this article.
The code behaves like this (Perl v5.28.2):
sub increment_counter {
my $counter = 0;
return sub
{
return ++$counter;
}
}
$c1 = increment_counter();
$c2 = increment_counter();
print &$c1(), "\n"; # -> 1
print &$c1(), "\n"; # -> 2
print &$c1(), "\n"; # -> 3
print &$c2(), "\n"; # -> 1
print &$c2(), "\n"; # -> 2
I changed (judging from browsing through the page history, it's probably a simple revert to some previous state) the code example to match the textual explanation:
{
my $counter = 0;
sub increment_counter {
return ++$counter;
}
}
This does what's described.
2A02:AB88:1541:8C00:5810:A015:AC18:6016 (talk) 13:25, 17 March 2021 (UTC)
In general, in C or C++, it is good form to parenthesize every parameter to a macro in its definition, as well as the entire definition. For example:
#define ADD_A(x) ((x) + a)
I'm not making the edit myself, because I'm not sure if it would add unnecessary complexity to the article. The example given does not have any actual bugs. To understand why this convention exists, please consider the following examples:
/* Bad smell */
#define PLUS(x, y) x + y
/* Idiomatic version */
#define BETTER_PLUS(x, y) ((x) + (y))
/* This is why parentheses are recommended around the whole expansion. */
a1 = w * PLUS(x, y); /* w and x are multiplied, then the result is added to y... probably unexpected! */
a2 = w * BETTER_PLUS(x, y); /* x and y are added, then w is multiplied by the result, so a2 has the expected value. */
/* This is why parentheses are recommended around the arguments when they occur in the expansion. */
a3 = PLUS(w = x, y); /* x is added to y, then the result is stored in w... probably unexpected! */
a4 = BETTER_PLUS(w = x, y); /* x's value is stored in w, then that same value is added to y afterwards to calculate a4, so w has the expected value. */
Having two different language constructs (function-like macros and actual functions) with the same syntax and different semantics invites error, but that's what C and C++ are like. Augmenting the language's actual definition with this convention makes the semantics of function-like macros more similar to those of functions at the point of use (though they are still not identical).
The page has two "Lexical scope vs. dynamic scope" sections. Need to eliminate one of them! — Preceding unsigned comment added by 131.119.15.14 (talk) 14:38, 9 June 2022 (UTC)
I do not understand the attempted distinction between module-level and global variables in Python. AFAIK, in Python global scope means module-level scope. See https://docs.python.org/3/reference/executionmodel.html Cerberus (talk) 19:41, 10 January 2026 (UTC)
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.