ArticleslgStudy

computer science

Local variable

Local variable is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Local variable rather than just read about it. In short: In computer science, a local variable is a variable that is given local scope. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope.

Key takeaways

  • Local variable belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Local variable to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Local variable from memory before moving on to harder problems.

Reference excerpt

In computer science, a local variable is a variable that is given local scope. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope. 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 nested levels of visibility, with private variables, functions, constants and types hidden within them, either by nested blocks or nested functions. Local variables are fundamental to procedural programming, and more generally modular programming: variables of local scope are used to avoid issues with side-effects that can occur with global variables.

Scope Local variables may have a lexical or dynamic scope, though lexical (static) scoping is far more common. In lexical scoping (or lexical scope; also called static scoping or static scope), if a variable name's scope is a certain block, then its scope is the program text of the block definition: within that block's text, the variable name exists, and is bound to the variable's value, but outside that block's text, the variable name does not exist. By contrast, in dynamic scoping (or dynamic scope), if a variable name's scope is a certain block, then its scope is that block and all functions transitively called by that block (except when overridden again by another declaration); after the block ends, the variable name does not exist. Some languages, like Perl and Common Lisp, allow the programmer to choose static or dynamic scoping when defining or redefining a variable. Examples of languages that use dynamic scoping include Logo, Emacs lisp, and the shell languages bash, dash, and the MirBSD Korn shell (mksh)'s "local" declaration. Most other languages provide lexically scoped local 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 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.

Static local variables A special type of local variable, called a static local, is available in many mainstream languages (including C/C++, Visual Basic, VB.NET and PHP) which allows a value to be retained from one call of the function to another – it is a static variable with local scope. 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 have the same lifetime as static global variables, because their value remains in memory for the life of the program, but have function scope (not global scope), as with automatic local variables. This is distinct from other usages of the static keyword, which has several different meanings in various languages.

Local variables in Perl Perl supports both dynamic and lexically-scoped local variables. The keyword local is used to define local dynamically-scoped variables, while my is used for local lexically-scoped variables. Since dynamic scoping is less common today, the Perl documentation warns that "local isn't what most people think of as “local”.". Instead, the local keyword 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. To create lexically-scoped local variables, use the my operator instead. To understand how it works consider the following code:

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 would be limited to the static scope of the function f() and not seen by g(). Randal L. Schwartz and Tom Phoenix argue that the operator local should have had a different name like save.

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). Note that Ruby only provides built-in support for statically-scoped local variables like Perl's my, not dynamically-scoped local variables like Perl's local. There is at least one library for Ruby that provides dynamically-scoped variables.

See also Dynamic variable Global variable Non-local variable

References

Worked examples

Example 1 — a first encounter with Local variable

Start with the simplest possible case. Write down what Local variable claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Local variable before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Local variable ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Local variable

In research
Local variable appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Local variable in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Local variable is common in secondary-school and first-year university syllabi. It links to neighbouring topics Variable (computer science), so understanding it makes those chapters shorter.
In everyday life
Look for Local variable outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Local variable” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Local variable in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Local variable means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Local variable out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Local variable in simple terms?

In computer science, a local variable is a variable that is given local scope. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope.

Why does Local variable matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Local variable?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Local variable.

Tags

  • Variable (computer science)

Keep exploring