Scope (Programming)

This article is about the use of the term in computer science. See scope for other uses. In computer programming, the scope of an identifier refers to where and when in the program the identifier can be referenced. Scope applies to all identifiers in a program, but it is mostly used for variables, functions and classes. For example, the scope of a variable refers to where in the program the variable can be accessed and/or modified. For a function, to where the function can be called, and so on. Common scope levels:
  • global scope: visible from the entire program
  • file scope: visible from a single source file.
  • local scope: visible only from the local function or block where the name was declared.
Scope is usually hierarchical: a variable declared at local scope (for example, as a local variable inside a function) supersedes the same variable declared at global scope. All references to this variable inside the function will manipulate the locally-defined variable. Outside the function, they will manipulate the globally-defined 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++.
  static const int max_number_of_users = 100;    public class User {      public:          static int number_of_users;               const char *name;             void User (const char *new_name) {              int n = number_of_users;              n++;              if (max_number_of_users < n)                  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

See also

 

<< PreviousWord BrowserNext >>
guillaume apollinaire
amedeo modigliani
pierre beaumarchais
comparative advantage
jean moulin
colonel bogey march
imre nagy
cop rock
american broadcasting company
cnn
big three
georges pierre seurat
simone signoret
simone de beauvoir
227 bc
cimetire du montparnasse
man ray
north island
mrs. miniver's problem
south island
carrier pigeon
religious prostitution
cher ami
static scoping
dynamic scoping
academy award for comedy direction
panthon, paris
irne joliot curie
gabrielle roy
harold robbins
228 bc
federal radio commission
uss housatonic (1861)
css hunley
arthur hailey
the adventures of robin hood (movie)
alexander's ragtime band
girls and boys town
robert bateman
the citadel
trotskyism
prince philip, duke of edinburgh
four daughters
grand illusion