Science Fair Projects Ideas - Scope (programming)

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

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

10-26-2009 08:16:03
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice