We’re on a mission to learn one programming language and work on all, previously we named some common and shared concepts among almost all programming languages.
Let’s revive the common concepts list:
- Variables
- Control Structures
- Data Structures
- Syntax
- Tools
Okay, so we are starting with:
Variables
Variables as a concept is the most fundamental part in all languages. You cannot find any programming language which is not implementing Variables as a core concept. Variables can be described as:
A storage location holding some information referred to as a value.
Clear? Not Yet?
Let’s say in other words: “Variables are places to keep data for later use.”
For example, if you try to comment below this post, I have put some textboxes to get your name, email and message. These textboxes take your information and list them for me later. Let us say the textbox asking for name (also represents variable; storing your name) is named as “yourName”, which is a symbolic name for your VARIABLE/Name.
So now, when you type your name inside the box, it would be stored in a variable called “yourName”. Since your name is stored in a variable, I can use it in multiple places to show your name. And every time someone types a name in the box, the name will be displayed on all occurrences where variable is used. According to the definition above: the text entered in textbox will be stored in a place called variable (yourName) and can be used later.
Again; Variables are storage locations holding some information (also called value) for later and multiple use. Variables are presented with a symbolic name called Identifier. e.g.
Variable Name (Identifier) | Value |
yourEmail | hi@razabayani.com |
st_name | Raza |
This concept is extremely powerful in programming and is used constantly. It is what makes Facebook and Twitter work, it’s what makes paying your bills via your online bank work, it’s what allows you to place a bid on eBay. Variables make the programming world go ’round.
Here a question should raise in your mind that: Are the variables implemented and used alike in all programming languages?
Same Concept, Different Implementations
I must say NO! As mentioned in previous article; most of the programming languages are created to debug other programming languages’ flaw, some of the creators found the flaw in the way that variables are implemented, therefore they changed it according to their mind. Hence, we have got the same concept of Variables but, with different implementations in different programming languages.
Because we have thousands of programming languages which implement the Variables’ concept differently and I can’t bring them all here, I prefer to classify the implementation of Variables, based on the type of programming language you use.
1- Statically Typed
Those programming languages which are strict to the type of the information you want to store inside a variable.
Consider the above example, if I strict the name textbox to be used only for storing strings, my variable “yourName” which stores the value, will only be able to store string values. In these type of programming languages, you must mention the type of data you want to store before the variable name.
Many well-known programming languages such as C, C++, Go, Haskell, Java, Kotlin, Nim, Rust, Scala, Swift and etc. use this type of implementation for Variables.
e.g., In Java we declare a variable as below:
int data;
data = 50;
data = “Hello World!”; // causes an compilation error
This way the variable “data” will be Integer till it exists.
2- Dynamically Typed
Some programming languages are free of these concerns. They check the type of data inserted into the variable at the last stage just to know the type but not to strict them. Thus, they don’t ask you to mention the type of data you want to store in variable, in-opposite they check and get the type when the data (value) is given to variable.
Popular Languages such as: PHP, JavaScript, Ruby, Python, Perl, Lua and etc. are included in this category.
e.g., In PHP we declare as below:
$s = \"abcd\"; // $s is a string
$s = 123; // $s is now an integer
$s = array(1, 2, 3); // $s is now an array
$s = new DOMDocument; // $s is an instance of the DOMDocument class
Data Types support also vary in respect to different languages, some common types are:
- String: to store a set of characters
- Integer: to store a whole number
- Float: to store number with floating-point
- Boolean: to store true/false conditions
- Char: to store a single character
Conclusion:
So, to sum up, we talked about what a variable is and how you can store information in a variable and then retrieve that information at some later point in time. The variable can have a name, and this name you give to the variable is usually named after the kind of content you’ll be storing in the variable, so if I’m storing your name in the variable, you’d name the variable ‘yourName’. You wouldn’t HAVE to give it that name, you could name the variable “badProgramming”, but that wouldn’t make a whole lot of sense considering you are trying to store a person’s name. Makes sense, right?
Finally, variables have types, and these types are used to help us organize what can and cannot be stored in the variable. Hint: having a type will help to open up what kind of things we can do with the information inside the variable.
Example: if you have two Integers (let’s say 50 and 32), you would be able to subtract one variable from the other (i.e. 50 – 32 = 18), pretty straight forward, right? But if you had two variables that stored names (i.e., “Ali” and “Hamid”) it wouldn’t make sense to subtract one from the other (i.e. “Ali” – “Hamid”), because that just doesn’t mean anything!
I hope all the information given above clarifies the concept of the topic “Variables”. These descriptions were to make this concept easier to you for all kinds of programming languages. Waiting to hear, if it was helpful to you.