Jump to content

Temporary variable

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Fmadd (talk | contribs) at 14:51, 4 February 2017. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer programming, a temporary variable is a variable whose purpose is short-lived, usually to hold temporary data that will soon be discarded, or before it can be placed at a more permanent memory location. Because it is short-lived, it is usually declared as a local variable, i.e., a variable with local scope. There is no formal definition of what makes a variable temporary, but it is an often-used term in programming.

A typical example would be that of swapping the contents of two variables. To swap the contents of variables a and b one would typically use a temporary variable temp as follows, so as to preserve the data from a as it is being overwritten by b:

 temp := a
 a := b
 b := temp

Temporary variables are usually named with identifiers that abbreviate the word temporary, such as temp, tmp or simply t, or with common metasyntactic variable names, the most common of which are foo, bar, baz (see also foobar).

Scope and Lifetime

The mechanism of scope is used to bound the lifetime of data; the local variables of an inner scope or function call can certainly be considered local from the perspective of the outer scope, or caller.

Memory hierarchy

Computer hardware is designed to exploit the behaviour of temporary data: a cache or register file may contain temporaries internally to a microprocessor, such that they never need to be committed to main memory (hence consuming no external memory bandwidth).

See also