biology daily - the biology and biochemistry encyclopedia
biology daily articles and research Encyclopedia Dictionary Forums biology research links Weblinks Pictures Articles Blogs Newsletter

Scope (programming)

This article is about the use of the term in computer science. See scope for other uses.

In computer programming, scope (or scoping) refers to the rules used by a particular programming language to determine what, if any, entity a given occurrence of an identifier in a program refers to. The entities in question can be values (as in functional languages like ML), locations where values might be stored (e.g. the local variables in a language such as C or Java), or other sorts of program constructs (such as functions and classes). The scope of a binding of an entity to a name is the portion of the program text where occurrences of that name can refer to that entity. In colloquial usage one often refers to this portion of program text as the scope of the name rather than of the binding; it is also common to say the binding is "visible from" this region of the program.

Among other possibilities, a binding may have:

  • global scope, meaning it is visible from the entire program.
  • file scope, meaning it is visible from a single source file (probably the one in which the binding appears).
  • local scope, meaning it is visible only from the local function or block where the binding appears.
  • (in object-oriented languages) class scope, meaning it is visible only within the class where the binding appears.
  • (in a library) external scope, meaning that the binding is visible from other programs which include this one.

The issue of scope arises partly because most programming languages allow a name to be bound to more than one different entity within the same program; languages must therefore have rules for determining, for a given occurrence of a name, which of two or more entities of the same name the occurrence refers to.

The scopes of bindings are usually hierarchical, meaning that the region of program text associated with one binding may be completely contained in the region associated with another. In most languages, when the location of an identifier occurrence is within the scope of more than one binding for that identifier, the occurrence refers to the binding with the smallest scope: hence, a variable declared at local scope (for example, as a local variable inside a function) supersedes any global variable of the same name. All occurrences of this name inside the function will refer to the locally variable; occurrences outside the function will refer to the global variable.

Because of this automatic scope handling, careful management of variable, class and function names should be a requirement for any non-trivial program. It is considered good programming practice to make variables as narrow a scope as feasible so that different parts of your program do not accidentally interact with each other by modifying each other's variables. This also prevents Action at a distance. Common techniques for doing so are to have different sections of your program use different namespaces, or else make individual variables private through either dynamic variable scoping or lexical variable scoping.

Example

This is an example in C++.

 const float PI = 3.14f; // global scope variable
 static const int max_number_of_users = 100; // file scope variable
 
 public class User {
     public:
         static int number_of_users; // class variable
    
         const char *name; // instance variable
 
         void User (const char *new_name) {
             int n = number_of_users; // local variable
             n++;
             if (max_number_of_users < n) {
                 int users = n; // local variable
                 cout << "There is not enough space for " << users<< " users." << endl;
                 abort ();
             }
             number_of_users = n;
             name = new_name;
         }
 }

In this example,

  • max_number_of_users - file scope variable
  • number_of_users - class variable
  • name - instance variable
  • new_name - local variable
  • users - The scope of this variable is limited to the if statement that it is declared within.

See also



05-27-2008 11:01:51
The contents of this article are licensed from Wikipedia.org under the GNU Free Documentation License. How to see transparent copy
BiologyDaily.com 2005. Legal info   Privacy