- Characteristics of programming variables
- Memory allocation
- Statement
- Scope
- Types of variables in programming
- Integer (short, long)
- Floating point (single, double)
- Character
- Boolean
- Fixed length string
- Variable length string
- Examples of programmatic variables
- Change a variable
- References
A variable in programming is a unit of data that can change its value. It is the simplest form of storage, representing a memory area where an item of data is stored.
If a computer program were a building, then the variables would be the building blocks that make up its foundation. Variables are critical components of any program. This could not be effective without variables.
Source: pixabay.com
One variable can be air temperature or stock prices. These are all values that can change.
The variables have two important purposes, which are that the programmer can choose the names of them, thus facilitating programming, and also that he can write programs or functions that work with any value in them.
If you are already familiar with spreadsheets, you might think that variables are like cells, which can then be used in formulas, regardless of the values they contain in them.
All procedural programming languages, such as C, Basic and Pascal have variables, being able to admit different types and allow to manipulate them in different ways.
Characteristics of programming variables
Memory allocation
A variable is the marker of a location in computer memory. When you create a new variable 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 memory of the computer. You should think of memory as if it were a block, when a program is executed it will have access to a block of memory.
Statement
When declaring a variable, you give it a name and also give it a type. In fact, the space is created where its value will be stored. 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 were declared, or also in any other function that is called by that function.
The scope is hierarchical and only applies top-down, from the main body of the program to the functions it calls, and from the functions to other sub-functions.
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 one 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 also have to declare what type of data it will contain. This is done because the program will use different types of data in different ways.
Integer (short, long)
This type corresponds to whole numbers, such as 1, -15, 0. Integer variables are used when it is known that there will never be anything after the decimal point. For example, if you are programming a lottery ball generator, all balls have whole numbers.
The difference between short integer and long integer types is the number of bytes used to store them.
This will vary according to 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 quantizers are analogous to the short and long quantizers 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.
Commonly seen in C programs, which cannot handle character strings. The value that is stored is actually an integer that represents the code (for example, ASCII) for the represented character.
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 constant.
Fixed length string
Strings are variables that contain text, and they come in two types. With a fixed-length string, you declare how many characters that string will contain.
Certain API calls in Windows require the use of fixed-length strings, but they are generally not used in Basic. In C they are implemented as an array of characters.
Variable length string
It is the one in which the length is not defined. This is the default type in Basic, and is useful for taking input from the user where you don't know what the response will be.
Examples of programmatic 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 keyword my. 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 character string: Written test, enclosed in double quotes. Double quotes indicate that the text inside is a character 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 a variable can be changed, swapped, or set to a different value. The following table gives 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.
- Computer Hope (2020). Variable. Taken from: computerhope.com.
- Techopedia (2020). Variable. Taken from: ceilingpedia.com.