Informatics    
   
Table of contents
(Prev) Local search (optimization)Locality of reference (Next)

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. In programming languages with only two levels of visibility, local variables are contrasted with global variables. On the other hand, many ALGOL-derived languages allow any number of levels of nested functions with private variables, functions, constants and types hidden within them.

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 distinct addresses. Hence variables of this scope can be declared, written to, and read, without any risk of side-effects to functions 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.

Variables of local scope are used to avoid issues with side-effects that can occur with global variables.

Contents

Static 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 (single, statically allocated) variable. In all of the above languages, static 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.[1] The only difference is that they are only accessible (i.e., scoped) to one function.

This is distinct from other usages of the static keyword, which has several different meanings in various 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”.[2] 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.[3]

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

To understand how it works consider the following code:

$a = 1;sub f() {  local $a;  $a = 2;  g();}sub g() {  print "$a\n";}g();f();g();

this will output:
1
2
1

this happens since the global variable $a is modified to a new temporary (local) meaning inside f(), but the global value is restored upon leaving the scope of f().

Using my in this case instead of local would have printed 1 three times since in that case the $a variable is really local to the scope of the function f() and not seen by g().
For this reason many consider that the operator local should have had a different name like save.[5]

Local variables in Ruby

Ruby as a language was inspired also by Perl, but in this case, the notation was made simpler: a global variable name must be preceded by a $ sign, like $variable_name, while a local variable has simply no $ sign in front of its name, like variable_name (while in perl all scalar values have a $ in front).

See also

References

  1. ^ Current C standard PDF (3.61 MB) (as of 2009[update]). In particular, see section 6.2.4 “Storage durations of objects”, page 32.
  2. ^ perldoc.perl.org: local
  3. ^ perldoc.perl.org: perlsub: Temporary Values via local()
  4. ^ perldoc.perl.org: perlsub: Private Variables via my()
  5. ^ Randal L. Schwartz and Tom Phoenix (2001-07-01). Learning Perl 3rd edition. O'REILLY. ISBN 0-596-00132-0. 
(Prev) Local search (optimization)Locality of reference (Next)