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:
“Happy is that mother whose ability to help her children continues on from babyhood and manhood into maturity. Blessed is the son who need not leave his mother at the threshold of the worlds activities, but may always and everywhere have her blessing and her help. Thrice blessed are the son and the mother between whom there exists an association not only physical and affectional, but spiritual and intellectual, and broad and wise as is the scope of each being.”
—Lydia Hoyt Farmer (18421903)
“The function of muscle is to pull and not to push, except in the case of the genitals and the tongue.”
—Leonardo Da Vinci (14251519)