Local variable: Difference between revisions

Content deleted Content added
No edit summary
Blanked the page
Line 1:
In [[computer science]], a '''local variable''' is a [[variable]] that is given ''local [[scope (programming)|scope]]''. Such a variable is accessible only from the [[Subroutine|function]] or [[block]] in which it is declared. Local variables are contrasted with [[global alpha beta segato 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-effect (computer science)|side-effects]].
 
==Static local variables==
{{main|Static variable}}
A special type of local variable, called a static local, is available in many mainstream languages, including [[C (programming language)|C]]/[[C++]], [[Visual Basic]] and [[Visual Basic .NET|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 a special ''storage class'' keyword (e.g., <code>static</code>).
 
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 programming|object-oriented]] languages.
 
Stricter and more formal [[Object-oriented programming|object-oriented]] languages such as [[Java (programming language)|Java]] and [[C Sharp (programming language)|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 <code>static</code> keyword, which has several different meanings in various other languages.
 
{{compu-prog-stub}}
[[Category:Programming constructs]]
 
[[fr:Variable locale]]
[[is:Staðvær breyta]]
[[ja:ローカル変数]]