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.
Read more about Local Variable: Static Local Variables, local
in Perl, Local Variables in Ruby
Famous quotes containing the words local and/or variable:
“Wags try to invent new stories to tell about the legislature, and end by telling the old one about the senator who explained his unaccustomed possession of a large roll of bills by saying that someone pushed it over the transom while he slept. The expression It came over the transom, to explain any unusual good fortune, is part of local folklore.”
—For the State of Montana, U.S. public relief program (1935-1943)
“There is not so variable a thing in nature as a ladys head-dress.”
—Joseph Addison (16721719)