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:
“As the creative adult needs to toy with ideas, the child, to form his ideas, needs toysand plenty of leisure and scope to play with them as he likes, and not just the way adults think proper. This is why he must be given this freedom for his play to be successful and truly serve him well.”
—Bruno Bettelheim (20th century)
“The press and politicians. A delicate relationship. Too close, and danger ensues. Too far apart and democracy itself cannot function without the essential exchange of information. Creative leaks, a discreet lunch, interchange in the Lobby, the art of the unattributable telephone call, late at night.”
—Howard Brenton (b. 1942)