Intro
In this chapter the concept of variables will be explored. By the end you should be able to recognise a variable and know why they are used.
Both Python and C++ will be used for code examples.
What Are Variables?
What is a variable? In computer programs we use variables to name a place in memory that contains a piece of data. Think of it as a box which has a label; that we can access at anytime and know it’s location.
So why do we use variables? Variables are used in programs so we can refer to pieces of data by name instead of a memory address. They allow us to store and retrieve that data at a later time.
FACT: Variables are an abstract storage location, as they refer to a memory address which contains the actual data.
FACT: Old computer programming languages had no concept of variables, instead they would read and manipulate specific memory addresses.
Naming
When naming a variable most languages follow “Naming Conventions” which are a set of rules that should be followed.
The following are some conventions that MUST be followed in most languages:
- Must start with
a-z
orA-Z
character - Cannot contain symbol characters such as
!
,+
, etc…- Some languages permit the use of the
_
character
- Some languages permit the use of the
Declaration
Before we can use a variable it has to be declared. Declaration is a process where we create our variable by giving it a name, data-type and value.
FACT: Depending on the language, data-types do not need to be defined
Let’s define a variable to hold our name, if you have read the data-types tutorial you should know that the data-type will be a String.
In a Python program we would declare our variable like this:
|
|
In C++ we would do something similar, however we would give the variable the correct type:
|
|
FACT: Python is a dynamic language so data-types in variables do not need to be specified.
Constants
So far we have learnt how we might declare a variable. Expanding on this, we may want to define a variable as a “Constant”. But what is a “Constant”?
A “Constant” is a variable that the programmer has specially marked; so the computer and any other humans know will never change. Some languages will prevent the variable’s data from being changed, essentially locking it from ever being modified. Think of it as a locked display case, we may be able to see the item however we cannot modify the item.
In most languages we would ensure the variable name is in UPPER_CASE
. See how the variable name uses _
to separate the words, this makes it easier to read for the programmer.
FACT: This naming convention is called “SCREAMING_SNAKE_CASE”.
For example in Python we may want to permanently store a location name:
|
|
Here is the same example in C++:
|
|
As you may have spotted, in C++ we place const
in-front. This tells the computer to handle this variable differently (as a constant). In Python there is no way to tell the computer that it is a constant. So all constant variables will be treated as a normal variable however, we can indicate to any programmers that the variable should never be modified.
A real world example for a constant could be the value of Pi:
PI = 3.142
Scope
There are two different scopes that can be found in programs: global and local. Think of scope as a way of sharing a variable with different parts/sections of a program.
A globally scoped variable can be accessed and changed throughout the whole program. Because of this the variables data will have to exist for the whole programs duration, which can potentially be a waste of memory.
Locally scoped variable on the other hand differ as they instead only exist in a specific section of the program. Locally scoped variables tend to be the preferred way of implementing variables as they are easier to debug, since they are confined to a specific piece of the program. Because of this they also don’t need to exist for the whole program’s duration; which can decrease the amount of memory needed, since when they are no longer required they can be removed from memory.
FACT: When a variable is longer needed it will go “out-of-scope”.
Global and local scopes will be demonstrated and explained further in a later tutorial.
Conclusion
In this chapter you should now be able to:
- Recognise a variable
- Decide whether a variable is a constant or not
- Why variables are needed
- Know the basics of global and local scopes