Temporary variable

From Wikipedia, the free encyclopedia

Page Module:Message box/ambox.css has no content.

In computer programming, a temporary variable is a variable with short lifetime,[1][2] usually to hold 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. Temporary variables, along with XOR swaps and arithmetic operators, are one of three main ways to exchange 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 := 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).

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

Lua error in mw.title.lua at line 404: bad argument #2 to 'title.new' (unrecognized namespace name 'Portal').

References

Page Template:Reflist/styles.css has no content.

  1. ^ Page Module:Citation/CS1/styles.css has no content.Gafoor, Rafael (2024). "Temporary Names, Variables and Files". A Comprehensive Guide to Coding and Programming in Stata. Taylor & Francis. pp. 6–10. doi:10.1201/9781003483779-2. ISBN 978-1-003-48377-9.
  2. ^ Page Module:Citation/CS1/styles.css has no content.Ng, Kelvin S.; Leckebusch, Gregor C. (2021). "A new view on the risk of typhoon occurrence in the western North Pacific". Natural Hazards and Earth System Sciences. 21 (2): 663–682. Bibcode:2021NHESS..21..663N. doi:10.5194/nhess-21-663-2021.