1. Using Variables in Programming
Introduction
This computer programming tutorial is designed to help you understand variables, which are a
central part of any programming language. Variables provide temporary storage for information that
will be needed during the lifespan of the computer program (or application).
Variables are also used in programming to transfer information from one part of the program to
another - for example to provide a place to lay out a document before printing it - as well as being a
place to store status information that might be needed by all parts of that program.
While some of the concepts might seem language-specific, they apply to all programming languages
to varying degrees, and the terminology is programming language-neutral.
A programmer can imagine a variable as being a box into which information can be placed, and the
shape of the box (the variables type) determines what kind of information it can store.
It is advised to read the Data Types Tutorial before this one, if the above statement is in any way
unclear.
Scope
In order to ensure that the right
information is modified during the
execution of the program, most
programming languages employ a concept
known as scoping.
The scope of a variable defines whether it
can be accessed and/or modified by a given part of the program. If it can be accessed, it is said to be
in scope, otherwise, it is said to be out of scope.
Local variables are those that are in scope within a specific part of the program (function,
procedure, method, or subroutine, depending on the programming language employed).
Local variables might be read only, or read-write, depending on how they were passed to the
program part. Those that are defined within the program part will be read-write.
Passing variables can be done by value (read-only), and the information can be read by the program
part (function, procedure, method, subroutine, etc.) but not modified.
To allow modification, they should be passed by reference. This passes a pointer to the variable,
rather than the value, and ensures that the variable is read-write, when in scope.
2. Global variables are those that are in scope for the duration of the programs execution. They can be
accessed by any part of the program, and are read-write for all statements that access them.
Care should be taken when using global variables, and it is wise to indicate their scope by prefixing
the variable name with 'g_', 'global_', or something similar.
Variable Types
Since data can take a variety of forms, variables need to be defined according to their data type . If a
programming language is strongly typed the variable needs to be defined, along with its type, before
use. Programming languages that are weakly typed generally adopt a definition for a variable
without this step (for example, Visual Basic and JavaScript).
In addition, we can define variables as scalars, having one discrete value in a predefined range
determined by their data type, or arrays, containing a list of scalar values of a specific dimension. If
this seems like an abstract idea, imagine a set of post office pigeon holes, each storing a piece of
data.
Finally, there are user defined types (UDTs) and abstract data types, (ADTs) which allow the
programmer to extend the programming language by defining their own data types.
We might, for example, prefer to use a TRUE/FALSE value rather than 1/0. To do this, we can define
a UDT BOOLEAN, which has two possible values TRUE, assigned to be the same as 1, and FALSE,
assigned to be the same as 0. Our UDT can then be used as if the BOOLEAN type is part of the
language.
An ADT is generally used to store a piece of data that has multiple parts for example a name and
address card, which might have three or four pieces of information (name, address, telephone) but
which we might like to contain within a single ADDRESS data type.
Once we define what the ADDRESS.name, ADDRESS.address and ADDRESS.telephone members
look like, we can create a data type ADDRESS that can be used as a form of container.
Finally, pointers are a special kind of variable which refer to the memory location of that variable. To
3. read or write the value, we just follow the pointer. They are useful in creating temporary references
to pieces of data that can be destroyed and re-formed during the programs life cycle.
Conclusion
Along with the Data Types tutorial, this gives a first step in dealing with temporary information
storage in a computer program. Understanding data types and variables is a key requirement for the
further use of any programming language, and everyone using these tutorials to learn programming
will need to make sure that they have a good grasp of the concepts involved.
Navigation Links
The Data Types TutorialBack to the General Programming Index
Mailing List
Stay informed - sign up to the mailing list!
http://suite101.com/variables-a982