In programming, a variable is a value that can change, depending on conditions or on information passed to the program. Generally, a computer program consists of instructions that tell the computer what to do and data that the program uses when it is running.
The data consists of constants or fixed values that never change and variable values (which are usually initialized to “0†or some default value because the actual values will be supplied by a program’s user). Usually, both constants and variables are defined as certain data types. We will talk about data types later, now let us learn more about variables.
Variables are used to store data that is likely to be reused and/or manipulated in a program.
Variables provide a way of labeling data so our programs can be understood more clearly by the reader and ourselves. Take a moment and think about a program that requires users to input their first name or age. So a programmer will use variables such as firstName or age as variables because this is not fixed data
You can think of a variable as a data container that you call when you need the data that is in it.
It is important to use descriptive words when naming a variable as it can get difficult to manage unknown variable names in large programs.
Assigning a value to a variable
In most programming languages, the equals sign/assignment operator, ‘=’, is used to assign a value to a variable with the variable name on the left side of the operator and the value on the right.
For example in Python:
name = ‘Altschool Africa’
Some languages have special keywords that come before declaring a variable like JavaScript:
const name = ‘AltSchool Africa’
Whenever we need to access the name in our application, we can easily use the variable name and manipulate it based on the data type of the value assigned to our variable.
Data Types
In computer programming, a data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. Data type also defines a set of values and a set of operations that can be applied to those values. In simple terms, a data type specifies which type of value a variable has.
Imagine we have two values: 4 and ‘Ade Tiger’ – We can easily find the square of 4 but the same operation cannot be carried out on ‘Ade Tiger’.
Some operations that can be applied to values of one data type obviously do not make sense when applied to values of another type.
When we try the wrong operations on data types, an error is thrown either on compilation or execution of our program.
Various Data Types
Most programming languages support the following data types:
Type Checking in Programming Languages
Type checking involves ensuring that operations carried out on values of each particular data type in a computer program are valid. A type error is thrown if an invalid operation is carried out.
For example, the code ‘x = 4 * ‘Ade Tiger’’ will throw an error because the multiplication operator can only be used with numbers.
There are two major methods of type checking: Static and Dynamic.
Static type checking is done at compile time – when a compiler translates the source code, type checking is done and an error is immediately thrown and the code doesn’t run. This is useful because type errors are caught early in the development cycle. Seen in C++, Java, C, Go, Typescript, etc
Dynamically typed programming languages do type checking at runtime – The program runs but throws an error when the mismatched type code block is executed. Seen in JavaScript, Python, and Ruby. This can make it quite difficult to catch type errors in development especially if the program is not properly tested.