A variable in programming it is a unit of data that can change its value. It is the simplest form of storage, representing a memory area where a data item is stored.
If a computer program were a building, then the variables would be the bricks that make up its foundation. Variables are critical components of any program. This could not be effective without variables.
A variable might be air temperature or stock prices. These are all values that can change.
Variables have two important purposes, which are that the programmer can choose the names of them, thus making programming easier, and also that they can write programs or functions that work with any value in them.
If you’re already familiar with spreadsheets, you might think of variables as cells, which can then be used in formulas, regardless of the values they contain.
All procedural programming languages, such as C, Basic, and Pascal, have variables, which can admit different types and allow them to be manipulated in different ways.
[toc]
Characteristics of programming variables
memory allocation
A variable is the marker of a location in the computer’s memory. When a new variable is created in a program, the program allocates the amount of memory based on the data type of the variable.
Therefore, it is a place in the computer’s memory. Memory should be thought of as a block, when a program runs it will have access to a block of memory.
Statement
Declaring a variable gives the variable a name and also gives it a type. In fact, the space where your value will be stored is created. Thus, to declare a variable in a program, you must indicate what type of variable it is.
Some programming languages require a variable to be declared before using it. Others allow you to define the value of a variable without having to declare it first.
Scope
The scope determines how far the value of a variable can be read or changed.
Global variables are those that can be used throughout the entire program. That is, its scope is the entire application.
Local variables can only be used in the function or procedure where they are declared, or also in any other function that is called by that function.
The scope is hierarchical and only applies downwards, from the main body of the program to the functions it calls, and from functions to other subfunctions.
Therefore, if a variable is declared at the top of the program, another variable with the same name cannot be declared in a function.
However, if you declare a variable in a function, you can declare another variable with the same name in another function. They will be different variables and may have different values.
Types of variables in programming
When a variable is created, you must also declare what type of data it will contain. This is done because the program will use different types of data in different ways.
Whole (short, long)
This type corresponds to integers, such as 1, -15, 0. Integer variables are used when you know there will never be anything after the decimal point. For example, if you are programming a lottery ball generator, all the balls have integer numbers.
The difference between the short integer and long integer types is the number of bytes used to store them.
This will vary depending on the operating system and hardware being used. Currently it can be assumed that an integer will be at least 16 bits and a long integer will be at least 32 bits.
floating point (single, double)
Floating point numbers, such as 3.2435, are those that contain fractional parts. The single and double quantifiers are analogous to the short and long quantifiers used with the integer type to indicate how many bits will be used to store the variable.
Character
This type represents the value of a character. For example, a letter of the alphabet, a digit, or a special symbol. It is used to store a single character of text.
It is commonly seen in C programs, which cannot handle strings. The value that is stored is actually an integer that represents the code (for example, ASCII) for the character represented.
boolean
A boolean variable can store one of the following two values: True or False. These are usually an integer, for example in Visual Basic False is 0 and True is -1. The values for True and False are constants.
fixed length chain
Strings are variables that contain text, and they come in two types. With a fixed-length string, you declare how many characters that string is to contain.
Certain API calls in Windows require the use of fixed-length strings, but they are not generally used in Basic. In C they are implemented as a character array.
variable length string
It is one in which the length is not defined. This is the default type in Basic, and is useful for taking user input where you don’t know what the response will be.
Examples of Program Variables
Below is an example of a variable used in the Perl programming language.
– my $compname = “Written Test”;
– print «Example seen, $compname»;
This example declares the variable named compname using the my keyword. The dollar sign ($) in the Perl programming language indicates that compname is the name of a variable, but the dollar sign is not part of the variable name.
In the first line of the program, the variable is assigned a value using the assignment operator “=”.
The value of compname is the string: Typed test, which is enclosed in double quotes. Double quotes indicate that the text within them is a string, but they are not part of the string data.
When the program runs, the print statement replaces the variable name $compname with its assigned value from the string, thus displaying the result: Example seen, Test written.
change a variable
There are many different ways that a variable can be changed, swapped, or set to a different value. The following table shows examples of how a variable could be changed, giving an explanation of what is being done.
References
Martin Gibbs (2020). Basics of Variables in C Programming. Taken from: study.com. Advanced Ict (2020). Variables & Data Structures in Programming. Taken from: advanced-ict.info. Future Learn (2020). Using data types and variables. Taken from: futurelearn.com. ComputerHope (2020). Variable. Taken from: computerhope.com. Techopedia (2020). Variable. Taken from: techopedia.com.