Scope Outside A Function
A declaration has global scope if it has effect throughout an entire program, or (in some languages) if it has effect from the point of its occurrence until the end of the source-file it occurs in. The latter is also called file scope. Variable names with global scope — called global variables — are frequently considered bad practice, at least in some languages; but global scope is typically used (depending on the language) for various other sorts of identifiers, such as names of functions, and names of classes and other data types. In the JavaScript snippet we saw above, the function-names square and sum_of_squares have truly global scope, while in the C snippet, the function-name sum_of_squares has file scope.
{ my $counter = 0; sub increment_counter { $counter = $counter + 1; return $counter; } }Some languages allow the concept of block scope to be applied, to varying extents, outside of a function. For example, in the Perl snippet at right, $counter is a variable name with block scope (due to the use of the my keyword), while increment_counter is a function name with global scope. Each call to increment_counter will increase the value of $counter by one, and return the new value. Code outside of this block can call increment_counter, but cannot otherwise obtain or alter the value of $counter.
As we saw above, function scope and block scope are very useful for avoiding name collisions, but even at global scope, many languages have mechanisms such as namespaces to help mitigate this problem.
Read more about this topic: Scope (computer Science)
Famous quotes containing the words scope and/or function:
“For it is not the bare words but the scope of the writer that gives the true light, by which any writing is to be interpreted; and they that insist upon single texts, without considering the main design, can derive no thing from them clearly.”
—Thomas Hobbes (1579–1688)
“The function of literature, through all its mutations, has been to make us aware of the particularity of selves, and the high authority of the self in its quarrel with its society and its culture. Literature is in that sense subversive.”
—Lionel Trilling (1905–1975)