Local variable

Local variable

In computer science, a local variable is a variable that is given "local scope". Such a variable is accessible only from the function or block 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 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 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.

tatic local variables

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 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 languages.

Stricter and more formal object-oriented languages such as 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 has a keyword, local, for “localizing” variables, but in this case, local isn't what most people think of as “local”. [ [http://perldoc.perl.org/functions/local.html perldoc.perl.org: local] ] It gives a temporary, dynamically-scoped 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. [ [http://perldoc.perl.org/perlsub.html#Temporary-Values-via-local() perldoc.perl.org: perlsub: Temporary Values via local()] ]

To create lexical variables, which are more like the automatic variables discussed above, use the my operator instead. [ [http://perldoc.perl.org/perlsub.html#Private-Variables-via-my() perldoc.perl.org: perlsub: Private Variables via my()] ]

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • local variable — noun (computing) A variable with its use restricted to a particular subprogram • • • Main Entry: ↑local …   Useful english dictionary

  • local variable — vietinis kintamasis statusas T sritis informatika apibrėžtis ↑Kintamasis, kurio vardas aprašytas bet kuriame programos bloke (funkcijoje, procedūroje). atitikmenys: angl. local variable ryšiai: dar žiūrėk – kintamasis palygink – globalusis… …   Enciklopedinis kompiuterijos žodynas

  • Non-local variable — In programming language theory, a non local variable is a variable that is not defined in the local scope. While the term can refer to global variables, it is primarily used in the context of nested and anonymous functions where some variables… …   Wikipedia

  • Local — generally means that which relates to a specific area or place, and is not vast or widespread.Local may also refer to:In medicine: * Local refers to a restricted part of the organism; such as a local anesthesiaIn computing: * Locale, a term used… …   Wikipedia

  • Variable local — Una variable local es, en informática, la variable a la que se le otorga un ámbito local. Tales variables sólo pueden accederse desde la función o bloque de instrucciones en donde se declaran. Las variables locales se contraponen a las variables… …   Wikipedia Español

  • Variable — A variable (pronEng|ˈvɛərɪəbl) is an attribute of a physical or an abstract system which may change its value while it is under observation. Examples include the height of a child, the temperature across a state, or the input to a function. This… …   Wikipedia

  • Local independence — is the underlying assumption of latent variable models. The observed items are independent of each other given an individual score on the latent variable(s). This means that the latent variable explains why the observed items are related to… …   Wikipedia

  • Variable de type RR Lyræ — Variable de type RR Lyrae Diagramme de Hertzsprung Russell avec les zones où se situent divers types d étoiles variables, parmi lesquelles les céphéides et les RR Lyrae (en vert). En astronomie, les RR Lyrae sont des étoiles variables, nommées d… …   Wikipédia en Français

  • Variable de type rr lyrae — Diagramme de Hertzsprung Russell avec les zones où se situent divers types d étoiles variables, parmi lesquelles les céphéides et les RR Lyrae (en vert). En astronomie, les RR Lyrae sont des étoiles variables, nommées d après l étoile prototype… …   Wikipédia en Français

  • Variable air volume — (VAV) is a technique for controlling the capacity of a heating, ventilating, and/or air conditioning (HVAC) system. The simplest VAV system incorporates one supply duct that, when in cooling mode, distributes approximately convert|55|F supply air …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”