Variables are used in computer programming in all kinds of programming languages. They are an essential part of each program, adding flexibility to it. But how do they work within a program?
Just as one would expect from the name "variable", this kind of object is a placeholder for any kind of value, like in mathematical equations. It is defined by a name and a type, similar to the elements of a mathematical set.
Similar to using mathematical variables in an equation in order to compute something, variables in computer programming are used in a program to control the logical flow or to compute a value as part of an algorithm.
Due to the placeholder nature, the program can be used over and over again with different values for its variables. Because of the resulting flexibility, similar programs don't have to be changed or rewritten just because they have to compute results for different values.
When a variable has been defined in the source code of a computer program, the computer reserves a certain amount of memory in its RAM where the value of the variable will be stored during the execution of the program.
The location of this part of reserved memory - the memory address - is linked to the name of the variable. So whenever the variable is used, the computer will jump to the associated memory address and read the value which is currently stored there, or overwrite it if the variable value is changed.
The size of the memory area that the computer reserves for the variable depends on the data type which is used in the definition of the variable.
Most data types are static during the execution of the program, the amount of memory they use usually cannot be changed during runtime. This also limits the range of values that a variable can have.
Each programming language has a set of basic data types which usually refer to the data types that the CPU can handle: bits, bytes, integer, characters, floating point values etc.
By using more bytes for some data types, a variable can hold a broader range of values - for example, a variable of the type "long integer" usually has the double amount of bytes that a standard "integer" variable has. This more than doubles the value range: each byte, the smallest amount of memory that a CPU can handle directly without any conversion, has eight digital bits and therefore can have 256 different values. For each byte that a one data type is longer than the similar previous one, the possible values are multiplied by 256.
All data types are build upon the binary system and the basic types that the CPUs can handle. This is true even if object types are used for the definition of a variable.
When the structure of the data type and its memory size are known, it's possible to convert or transfer variable values between variables of different types. If the memory size of different data types does not match, this can lead to memory errors and to wrong variable values if you are not careful.
Most programming languages have their own conversion functions for converting or transferring values between variables of the different data types.
The different standard data types - which can be used to create your own, more complex data types - need a different amount of memory in order to store the value of the related variable. The computer assigns a specific portion of the memory starting at the next free memory address when the program is running, and the amount of reserved memory for the variable depends upon the data type of the variable.
Here are some standard types from C and C++ with their memory usage and the resulting amount of different values that they can represent:
|Amount of possible values||Remarks|
|8||256||equal to 2 to the power of 8|
|32||4 294 967 296||more than 4 billion values|
|64||18 446 744 073 709 551 616||that's around 18 billion billion possible values!|
With the help of the basic data types most programming languages allow the definition of structures. These named structures can then be used as advanced data types for the definition of further variables.
Each structure contains one or more different variables which can be defined either with the basic data types or with other structure data types. To access one of the variables of the structure, it is usually necessary to use the name of the variable which has been defined using the structure type and combine it with a reference sign (often just the minus sign) and the name of the variable that is part of the structure.
A complete table can be defined for use inside the computer's memory by defining a list of similar variables which use the same structure type. Then the structure type is similar to the definition of the fields for a row of he table.
When a variable is defined as in instance of an object class, this is similar to using a structure as data type for the variable. The difference is that an object (object instance) not only contains variables of different data types which can store values, but also references to functions - the methods that are part of the object class - that can be used as part of the object.
Consequently, objects usually need more memory than the more basic data types.
On some occasions, variables are needed only as a way to reserve a certain amount of memory in the computer's RAM, with no up front definition of the structure and type of the data that will be stored there.
For this, some programming languages offer different ways to allocate and reserve memory blocks of a given size.
In contrast to standard data types or structures, the program has to contain some functionality to read data from inside the reserved memory block, write data to it or convert data to or from it - all the functions which are included in the programming language for the basic data types.
Last not least variables in a computer program can also be defined as pointers, at least in some programming languages.
When this is done, the variable does not directly reference a certain value stored at a location inside the computer's memory. Instead, it only contains the memory address where a value is stored. This then can be used to access any value stored there.
This can be used to create a dynamic, in-memory structure like a linked list or a binary tree.
Without variables, computer programs would have to be changed or rewritten constantly. Due to the advances in computer technology, memory size etc. we can use variables to store and manage all kinds of data today, from simple integer values to texts to images and videos.
They are as essential to computer programming as they are for mathematical calculations of any kind.