11.07.2015 Views

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

VvariableVirtually all computer programs must keep track <strong>of</strong> a variety<strong>of</strong> items <strong>of</strong> information that can change as a result <strong>of</strong>processing. Such values might include totals or subtotals,screen coordinates, the current record in a database, or anynumber <strong>of</strong> other things. A variable is a name given to sucha changeable quantity, <strong>and</strong> it actually represents the area <strong>of</strong>computer memory that holds the relevant data.Consider the following statement in the C language:int Total = 0;Variables have several attributes. First, every variablehas a name—Total in this case. Although this name actuallyrefers to an address in memory, in most cases the programmercan use the much more readable name instead <strong>of</strong>the actual address.It is possible to have more than one name for the samevariable by having another variable point to the first variable’scontents, or by declaring a “reference” variable (seepointers <strong>and</strong> indirection).Each variable has a data type, which might be number,character, string, a collection (such as an array), a datarecord, or some special type defined by the programmer(see data types). With some exceptions (see scriptinglanguages) most modern programming languages requirethat the programmer declare each variable before it is used.The declaration specifies the variable’s type—in the currentexample, the type is int (integer, or whole number).A variable is usually given an initial value by using anassignment statement; in the example above the variableTotal is given an initial value <strong>of</strong> 0, <strong>and</strong> the assignment iscombined with the declaration. (Some languages automaticallyassign a default value such as 0 for a number or a nullcharacter for a string, but with other languages failure toassign a value results in the variable having as its valuewhatever happens to be currently stored in the memoryaddress associated with the variable. An explicit assignmentis thus always safer <strong>and</strong> more readable.)When exactly do variables get set up, <strong>and</strong> when do theyget their values? This varies with the programming language(see binding). With C <strong>and</strong> similar languages, a variablereceives its data type when the program is compiled(compile time). The type in turn determines the range <strong>of</strong>values that the variable can hold (physically based on thenumber <strong>of</strong> bytes <strong>of</strong> memory allocated to it). The variable’svalue is actually stored in that location when the program isexecuted (run time).A few languages such as APL <strong>and</strong> LISP use dynamicbinding, meaning that a data type is not associated with avariable until run time. This makes for flexibility in programming,but at some cost in efficiency <strong>of</strong> storage <strong>and</strong>execution speed.During processing, a variable’s value can changethrough the use <strong>of</strong> operators in expressions (see operators<strong>and</strong> expressions). Thus, the example value Total might bechanged by a statement such as:Total = Total + Subtotal;When this statement is executed, the following happens:The value <strong>of</strong> the memory location labeled “Subtotal” isobtained.490

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!