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:
“In the works of man, everything is as poor as its author; vision is confined, means are limited, scope is restricted, movements are labored, and results are humdrum.”
—Joseph De Maistre (17531821)
“To look backward for a while is to refresh the eye, to restore it, and to render it the more fit for its prime function of looking forward.”
—Margaret Fairless Barber (18691901)