All Topics  
Local variable

 

   Email Print
   Bookmark   Link






 

Local variable



 
 
In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a local variable is a variable
Variable

A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e....
 that is given local scope
Scope (programming)

In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes....
. Such a variable is accessible only from the function
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
 or block
Statement block

In computer programming, a statement block is a section of computer code which is grouped together, much like a paragraph; such blocks consist of one, or more, statement ....
 in which it is declared. Local variables are contrasted with global variables.

In most languages, local variables are automatic variables stored on the call stack
Call stack

In computer science, a call stack is a dynamic Stack data structure that stores information about the active subroutines of a computer program....
 directly. This means that when a recursive function
Recursion (computer science)

Recursion is a way of thinking about and solving problems. In fact, Recursion_ is one of the central ideas of computer science. Solving a problem using recursion means the solution depends on solutions to smaller instances of the same problem....
 calls itself, local variables in each instance of the function are given separate memory address space
Address space

In computing, an address space defines a range of discrete addresses, each of which may correspond to a physical or virtual memory register, a Node , peripheral device, disk sector or other logical or physical entity....
.






Discussion
Ask a question about 'Local variable'
Start a new discussion about 'Local variable'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a local variable is a variable
Variable

A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e....
 that is given local scope
Scope (programming)

In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes....
. Such a variable is accessible only from the function
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
 or block
Statement block

In computer programming, a statement block is a section of computer code which is grouped together, much like a paragraph; such blocks consist of one, or more, statement ....
 in which it is declared. Local variables are contrasted with global variables.

In most languages, local variables are automatic variables stored on the call stack
Call stack

In computer science, a call stack is a dynamic Stack data structure that stores information about the active subroutines of a computer program....
 directly. This means that when a recursive function
Recursion (computer science)

Recursion is a way of thinking about and solving problems. In fact, Recursion_ is one of the central ideas of computer science. Solving a problem using recursion means the solution depends on solutions to smaller instances of the same problem....
 calls itself, local variables in each instance of the function are given separate memory address space
Address space

In computing, an address space defines a range of discrete addresses, each of which may correspond to a physical or virtual memory register, a Node , peripheral device, disk sector or other logical or physical entity....
. Hence variables of this scope can be declared, written to, and read, without any risk of side-effects to processes outside of the block in which they are declared.

Programming languages that employ call by value semantics provide a called subroutine with its own local copy of the arguments passed to it. In most languages, these local parameters are treated the same as other local variables within the subroutine. In contrast, call by reference and call by name semantics allow the parameters to act as aliases of the values passed as arguments, allowing the subroutine to modify variables outside its own scope.

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

Static local variables

A special type of local variable, called a static local, is available in many mainstream languages, including C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
/C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
, Visual Basic
Visual Basic

'Visual Basic' is the third-generation programming language event-driven programming and integrated integrated development environment from Microsoft for its Component Object Model programming model....
 and VB.NET
Visual Basic .NET

Visual Basic , formerly called Visual Basic .NET , is an object-oriented programming computer language that can be viewed as an evolution of Microsoft Visual Basic implemented on the .NET Framework....
, 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 a special storage class keyword (e.g., static).

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 object-oriented
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
 languages.

Stricter and more formal object-oriented
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
 languages such as Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
 and C#, do not allow local variables to be declared static to a function. Instead, "static" variables in these languages are scoped to the class.

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

local in Perl


Perl
Perl

In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
 has a keyword, local, for “localizing” variables, but in this case, local isn't what most people think of as “local”. It gives a temporary, dynamically-scoped
Scope (programming)

In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes....
 value to a global (package) variable, which lasts until the end of the enclosing block. However, the variable is visible to any function called from within the block.

To create lexical variables, which are more like the automatic variables discussed above, use the my operator instead.