Local variable

From Wikipedia, the free encyclopedia

In computer science, a local variable is a variable that is given local scope. Such variables are accessible only from the function or block in which it is declared. Local variables are contrasted with global variables.

Local variables are special because in most languages they are automatic variables stored on the call stack directly. This means that when a recursive function calls itself, local variables in each instance of the function are given separate memory address space. Hence variables of this scope can be declared, written to, and read, without any risk of side-effects to processes outside of the block they are declared.

Some advocate that all variables should be of local scope to avoid issues with side-effects.

Static local variables

Main article: Static variable

A special type of local variable, called a static local, is available in many mainstream languages, including C/C++, Visual Basic and VB.NET, which allows a value to be retained from one call of the function to another. In this case, recursive calls to the function also have access to the variable. In all of the above languages, variables are declared as such with the static keyword.

Static locals in global functions can be thought of as global variables, because their value remains in memory for the life of the program. The only difference is that they are only accessible through one function. Static locals can also be declared in class-level functions in the above object-oriented languages, and the behaviour differs depending on the language:

  • In C++, static locals declared in class-level functions are shared across all objects. That is, they act like static class-level variables.
  • In Visual Basic and VB.NET, static locals declared in class-level functions are local to the object. That is, they act like non-static class-level variables, and each object has its own copy of the variable.

Stricter and more formal object-oriented languages such as Java and C#, do not allow local variables to be declared static at all.

Note: This is distinct from other usages of the static keyword, which has several different meanings in various other languages.

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.