Sunday, February 17, 2019

Variables - Let's give names to things we want to remember

What are Variables?

A bit of mathematics.... Oh! Don't want it this way... OK
Let us call you, the reader, Mr. XYZ.
Hello Mr. XYZ, how have you been? Looks like you want to know what variables are.

       Variable, by the literal sense, means a value which can change. Isn't everything changing then? Aren't we in the world which is varying every second as we talk? Still, there are things which are referred to as constants. Constants refer to those values which do not change over time. Some famous ones being 𝝅,e,𝜾(iota). This was bit like mathematics. I tell you a thing for sure. Variables in Maths and computers don't behave as same. Can I stop being more boring?

       Let me not bore you anymore and delve into the computational terminology. Do these heavy words not scare you, Mr. XYZ? Don't you feel intimidated by this? Well, you should be. You are going to learn the hardest topic you would ever come across.

Let us consider your age to be 16.
=> age = 16

No. of days in a year are:
=>no_of_days_year  =  365 (Those who have their birthdays on 29th Feb, don't be harsh on me) 

No. of hours per day :
=> hours_per_day = 24
Let us consider your sleeping hours to be 12 hours a day. You sleep a lot Mr. XYZ. You need to wake up.
=> sleeping_hours_per_day = 12

=> time_spent_on_earth = age*no_of_days_year*hours_per_day = 16*365*24 = 140160
=> time_spent_sleeping = age*no_of_days_year*sleeping_hours_per_day  = 16*365*12 = 70080
You spent a whopping 70080 hours sleeping.
We did nothing extraordinary above. We assigned every value to a name which made sense and tried to formulate the time which you spent sleeping. This is what variables do. They are just references to the values we store and can access using just that name in future for any work we do.

Let us now look at the formal definition of a variable: Something big is coming!!!
In computer programming, a variable is a storage location (identified by memory address) paired with an associated symbolic name(an identifier), which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value, in addition to referring to the variable itself, depending on the context. This separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer source code can be bound to a value during run time, and the value of the variable may thus change during the course of execution.

 If you at least read each word of the definition, I salute you. Rest of the people, being lazy may help you but not this time. Try making sense of the definition.

Let's break the definition part by part:

Storage location: A part of the memory where we target to store data values and these have unique addresses.

Identifier: These are the symbolic names to the variables, given, so that programmers are at ease and don't have to directly play with memory addresses. They can use this name for using the variable and don't have to take care of how the processing works. Just like I called you Mr. XYZ. I draw your attention just by calling you Mr. XYZ. I don't have to take care of what your real name is.

Separation of content and value: This has its own advantages. You just have to know what the variable's purpose is. You don't even have to know what value it stores. Therefore, you can focus on the code rather than the minor details.

I assumed your age to be 16. But now you grew up and want your age to be 17.
You just change it at one place and WOAH!! Everywhere you use the variable it changed.
=>age = 17
Imagine if we directly played with values then every place 'age' was used, values would have to be changed.

Let us look at the computer level implementation now.

This is a part of the memory where there are memory blocks with the unique address.
Now I want to store an integer valued data which takes 4 bytes on  a normal modern system.


// C code

int a;    // variable declaration
(We tell computer's memory manager(MM) to allocate those four blocks of memory for this particular integer type data. Now MM will order four blocks of memory to be allocated and be given a symbolic name "a".)

What happens inside the memory looks something like this:


a = 10; // a stores 10
Now these four blocks store value 10. Anywhere we want to use this value 10, we use its alias "a". We neither care of the addresses '201', '202', '203' and '204' nor of the value 10 now.
We want to add 10 to the value of a:
=> a = a+10;
"a" stores 20 now.

No comments:

Post a Comment