SlideShare a Scribd company logo
1 of 3
Download to read offline
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.
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
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

More Related Content

Viewers also liked

Hoe leef je de leeftijdsgrens voor alcoholhoudende drank het beste na in de P...
Hoe leef je de leeftijdsgrens voor alcoholhoudende drank het beste na in de P...Hoe leef je de leeftijdsgrens voor alcoholhoudende drank het beste na in de P...
Hoe leef je de leeftijdsgrens voor alcoholhoudende drank het beste na in de P...Martijn Planken
 
3rd Midlands BI Forum Presentation sample
3rd Midlands BI Forum Presentation sample3rd Midlands BI Forum Presentation sample
3rd Midlands BI Forum Presentation sampleRory Allen
 
Ihenacho_Leadership Philosophy
Ihenacho_Leadership PhilosophyIhenacho_Leadership Philosophy
Ihenacho_Leadership PhilosophyIfeanyi Ihenacho
 
Ihenacho_Leadership Philosophy
Ihenacho_Leadership PhilosophyIhenacho_Leadership Philosophy
Ihenacho_Leadership PhilosophyIfeanyi Ihenacho
 
Ihenacho_Leadership Philosophy
Ihenacho_Leadership PhilosophyIhenacho_Leadership Philosophy
Ihenacho_Leadership PhilosophyIfeanyi Ihenacho
 
Scala Meetup Hamburg - Spark
Scala Meetup Hamburg - SparkScala Meetup Hamburg - Spark
Scala Meetup Hamburg - SparkIvan Morozov
 
Walsh Work Samples
Walsh Work SamplesWalsh Work Samples
Walsh Work SamplesJohn Walsh
 
Carta capital 01 set 2010 3
Carta capital 01 set 2010 3Carta capital 01 set 2010 3
Carta capital 01 set 2010 3Luis Nassif
 
Cartilha Tecnologos - CONFEA/ANT
Cartilha Tecnologos - CONFEA/ANTCartilha Tecnologos - CONFEA/ANT
Cartilha Tecnologos - CONFEA/ANTVictor Couto Alves
 
PST Fusionieren Datei
PST Fusionieren Datei PST Fusionieren Datei
PST Fusionieren Datei pattrikmaliki
 
chariteam - Angebote fuer Unternehmen
chariteam - Angebote fuer Unternehmenchariteam - Angebote fuer Unternehmen
chariteam - Angebote fuer UnternehmenchariteamDE
 
Trastorno limite de la pesonalidad
Trastorno limite de la pesonalidadTrastorno limite de la pesonalidad
Trastorno limite de la pesonalidadvitriolum
 

Viewers also liked (20)

Hoe leef je de leeftijdsgrens voor alcoholhoudende drank het beste na in de P...
Hoe leef je de leeftijdsgrens voor alcoholhoudende drank het beste na in de P...Hoe leef je de leeftijdsgrens voor alcoholhoudende drank het beste na in de P...
Hoe leef je de leeftijdsgrens voor alcoholhoudende drank het beste na in de P...
 
3rd Midlands BI Forum Presentation sample
3rd Midlands BI Forum Presentation sample3rd Midlands BI Forum Presentation sample
3rd Midlands BI Forum Presentation sample
 
Ihenacho_Leadership Philosophy
Ihenacho_Leadership PhilosophyIhenacho_Leadership Philosophy
Ihenacho_Leadership Philosophy
 
Ihenacho_Leadership Philosophy
Ihenacho_Leadership PhilosophyIhenacho_Leadership Philosophy
Ihenacho_Leadership Philosophy
 
ihenacho_Writing_Sample
ihenacho_Writing_Sampleihenacho_Writing_Sample
ihenacho_Writing_Sample
 
Ihenacho_Leadership Philosophy
Ihenacho_Leadership PhilosophyIhenacho_Leadership Philosophy
Ihenacho_Leadership Philosophy
 
Scala Meetup Hamburg - Spark
Scala Meetup Hamburg - SparkScala Meetup Hamburg - Spark
Scala Meetup Hamburg - Spark
 
Walsh Work Samples
Walsh Work SamplesWalsh Work Samples
Walsh Work Samples
 
ihenacho_Writing_Sample
ihenacho_Writing_Sampleihenacho_Writing_Sample
ihenacho_Writing_Sample
 
Ch10
Ch10Ch10
Ch10
 
Adenomyosis
AdenomyosisAdenomyosis
Adenomyosis
 
Medikal teknoloji
Medikal teknolojiMedikal teknoloji
Medikal teknoloji
 
Carta capital 01 set 2010 3
Carta capital 01 set 2010 3Carta capital 01 set 2010 3
Carta capital 01 set 2010 3
 
Cartilha Tecnologos - CONFEA/ANT
Cartilha Tecnologos - CONFEA/ANTCartilha Tecnologos - CONFEA/ANT
Cartilha Tecnologos - CONFEA/ANT
 
Dos Israel = dos Jerusalén, ¿CUÁL JERUSALÉN SERÁ HOLLADA Y CUÁL JERUSALEN NO ...
Dos Israel = dos Jerusalén, ¿CUÁL JERUSALÉN SERÁ HOLLADA Y CUÁL JERUSALEN NO ...Dos Israel = dos Jerusalén, ¿CUÁL JERUSALÉN SERÁ HOLLADA Y CUÁL JERUSALEN NO ...
Dos Israel = dos Jerusalén, ¿CUÁL JERUSALÉN SERÁ HOLLADA Y CUÁL JERUSALEN NO ...
 
PST Fusionieren Datei
PST Fusionieren Datei PST Fusionieren Datei
PST Fusionieren Datei
 
chariteam - Angebote fuer Unternehmen
chariteam - Angebote fuer Unternehmenchariteam - Angebote fuer Unternehmen
chariteam - Angebote fuer Unternehmen
 
Trastorno limite de la pesonalidad
Trastorno limite de la pesonalidadTrastorno limite de la pesonalidad
Trastorno limite de la pesonalidad
 
Rechtsruck
RechtsruckRechtsruck
Rechtsruck
 
Im sommer
Im sommerIm sommer
Im sommer
 

Similar to Using Variables in Programming

Similar to Using Variables in Programming (20)

Variables And Data Types
Variables And Data TypesVariables And Data Types
Variables And Data Types
 
Unit 1
Unit 1Unit 1
Unit 1
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
Data
DataData
Data
 
Cp 111 lecture 3
Cp 111 lecture 3Cp 111 lecture 3
Cp 111 lecture 3
 
Computer programing 111 lecture 3
Computer programing 111 lecture 3Computer programing 111 lecture 3
Computer programing 111 lecture 3
 
Programming concepts By ZAK
Programming concepts By ZAKProgramming concepts By ZAK
Programming concepts By ZAK
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Introduction to Visual Basic
Introduction to Visual Basic Introduction to Visual Basic
Introduction to Visual Basic
 
PCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxPCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docx
 
Ndu06 typesof language
Ndu06 typesof languageNdu06 typesof language
Ndu06 typesof language
 
Programming
ProgrammingProgramming
Programming
 
INTERPRETER AND APPLIED DEVELOPMENT ENVIRONMENT FOR LEARNING CONCEPTS OF OBJE...
INTERPRETER AND APPLIED DEVELOPMENT ENVIRONMENT FOR LEARNING CONCEPTS OF OBJE...INTERPRETER AND APPLIED DEVELOPMENT ENVIRONMENT FOR LEARNING CONCEPTS OF OBJE...
INTERPRETER AND APPLIED DEVELOPMENT ENVIRONMENT FOR LEARNING CONCEPTS OF OBJE...
 
Typeful programming
Typeful programmingTypeful programming
Typeful programming
 
eMedRec Notes - design basics
eMedRec Notes - design basicseMedRec Notes - design basics
eMedRec Notes - design basics
 
Java-centered Translator-based Multi-paradigm Software Development Environment
Java-centered Translator-based Multi-paradigm Software Development EnvironmentJava-centered Translator-based Multi-paradigm Software Development Environment
Java-centered Translator-based Multi-paradigm Software Development Environment
 
Htmll
HtmllHtmll
Htmll
 
Htmll
HtmllHtmll
Htmll
 
Mcs024
Mcs024Mcs024
Mcs024
 
Visual Programming
Visual ProgrammingVisual Programming
Visual Programming
 

Using Variables in Programming

  • 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