Local variable: Difference between revisions

Content deleted Content added
m links editing
Bluebot (talk | contribs)
m Fixing C++ links
Line 7:
==Static local variables==
{{seemain|static variable}}
A special type of local variable, called a static local, is available in many mainstream languages, including [[C programming language|C]]/[[C plus plus|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 the <code>static</code> 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 programming|object-oriented]] languages, and the behaviour differs depending on the language:
*In [[C plus plus|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 [[Visual Basic .NET|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.