DEV Community

Cover image for Variables in programming
Anwar Sadat Ayub
Anwar Sadat Ayub

Posted on • Updated on

Variables in programming

Hi there 👋,

In this article, I will discuss what a variable is and some of it's associated features.

Variable, as defined by Wikipedia

is an abstract storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value; or in simpler terms, a variable is a container for a particular set of bits or type of data (like integer, float, String etc.)

Maybe this a bit hard to digest. Let's try and break down into simple terms.

Consider your living room. Before you moved in, the room was empty hence enough space [of certain unit i.e. yard, foot, meter, etc.] to put your bed, wardrobe, shoe rack, desk for your computer, books and other stationary and even the space on the wall for your TV and wall clock. Remember this space is limited meaning that whenever you put an item in this space, the size of the space reduces based on the unit the item covers. The bed inside your room is taking a certain amount of space probably the biggest space. And also some items can be easily moved to get more space while other items will be remain at the same location for long e.g. bed and the TV.

Now let's consider the memory in our computers. Let's say a computer has a memory of size 1024 [Bytes of units] as illustrated below 👇

Random Access Memory of 1024 Bytes

Fundamentally, 1 byte = 1 character so the name John has 4 characters = 4 bytes, has 11 characters = 11 bytes. Hope that makes sense.

Our RAM is now empty with a full space enough to hold 1024 characters. As data is stored in the memory, then it's available space reduces. So out of the 1024 available space, when you store the name John, it will occupy 4 bytes leaving you with approximately 1020 bytes.

Programming involves receiving data, manipulating that data then display the output. Let's deal with the first part; receiving data. Supposed we want to develop an app that will determine if a person is eligible to vote. A person can vote if their age is 18 and above.

In our app, we will require the user to enter only two things;

  • Full Name
  • Age

First, we have to allocate space in our memory[abstract storage location] in anticipation of the full name and age that the user will enter as illustrated below 👇

Allocation of memory space

When we allocate memory space, we give it a name [an associated symbolic name] for easy reference. We now have two memory allocations that can be identified as Full Name and Age which are currently empty [waiting for an unknown value].

If the user enters "John Smith", then the Full Name space allocation will now be occupied with "John Smith" [known quantity of information] as indicated in the illustration below 👇;

Full Name variable with a value of "John Smith"

Similarly if the user enters 18 as the age, the memory allocation labelled as Age will have a known value of 18.

Age variable with a value of

Hence a variable in programming is a space that is allocated in a memory with a symbolic name that is used to store data. Variable is equally important in programming just as data.

Properties of variable

Naming a variable

Typically a variable has a *name * which is used to identify the space allocated in the memory. Because no programming language is reference in this article, I will only discuss what is generally accepted.

  1. The first character of a variable name must be an alphabet. Some programming language allow the use of underscore [ _ ]. Read the documentation to be sure of which characters are allowed
  2. A variable name must not include spaces e.g. full name is wrong but fullname, fullName, full_name, FullName are all acceptable.
  3. A variable name must not include some special character like pound sign [#], question mark [?],slashes [/ ], percentage sign [%] some few other characters.
  4. Keywords like _print, echo, cout, print_r, string, int, double, true, false, etc _ in various programming languages cannot be use as variable names.
  5. Variable names are generally case sensitive. fullName, FullName and fullname are not the same.

Another note on naming a variable is that it should be descriptive. Try not to use names like DOB to name a variable that holds Date of Birth.

Variable Type

Every variable has a type and the type will depend on the value it holds. Some programming language strictly enforce the use of variable type to hold only it's appropriate value [statically typed language] while other language will allow any value to be assign to the variable [dynamically typed language].

Variable type can be categorize into primitive and user-defined. Let's look at the primitive data type.
Some primitive data types are string, integer, double/float, boolean

String: it contains values that are placed between the quotation marks i.e. " or '. It's values are series of characters from alphabet, numbers and any special characters. To identify data as a string, simply put them between double or single marks e.g.

are all strings.

Integer: It takes integer values such 20, 100, -45, etc. Mathematical operations can be performed on this type of variable. Think about age, population, total number of fruits, number of countries in a continent, etc. All these will serve as good examples of integer values

Double/Float: Both take decimal numbers such as 101.2, 5.0, -1.165, 3.142, etc. Mathematical operations can be performed on this type of variable. Main difference between Double and Float is in the size of value allowed.
Boolean:it holds one of two values only i.e. true or false.

There is a whole lot that you can read about variable. I hope this article will help clear some confusion faced by beginners on variables.

In my next article, I will discuss about sequence structure so see you on the next one 👋.

Thank you.

Top comments (0)