SlideShare a Scribd company logo
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

What's hot

AACL 2018 - Going Beyond Simple Word-list Creation Using CasualConc
AACL 2018 - Going Beyond Simple Word-list Creation Using CasualConcAACL 2018 - Going Beyond Simple Word-list Creation Using CasualConc
AACL 2018 - Going Beyond Simple Word-list Creation Using CasualConc
yasuimao
 
Language design and translation issues
Language design and translation issuesLanguage design and translation issues
Language design and translation issues
SURBHI SAROHA
 
External domain-specific languages
External domain-specific languagesExternal domain-specific languages
External domain-specific languages
Mikhail Barash
 
Introduction to programing languages part 3
Introduction to programing languages part 3Introduction to programing languages part 3
Introduction to programing languages part 3
university of education,Lahore
 
Introduction to programing languages part 2
Introduction to programing languages   part 2Introduction to programing languages   part 2
Introduction to programing languages part 2
university of education,Lahore
 
Translation memory
Translation memoryTranslation memory
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data Types
Micheal Ogundero
 
C language
C languageC language
C language
Rupanshi rawat
 
XML | Computer Science
XML | Computer ScienceXML | Computer Science
XML | Computer Science
Transweb Global Inc
 
Projectional editing
Projectional editingProjectional editing
Projectional editing
Mikhail Barash
 

What's hot (10)

AACL 2018 - Going Beyond Simple Word-list Creation Using CasualConc
AACL 2018 - Going Beyond Simple Word-list Creation Using CasualConcAACL 2018 - Going Beyond Simple Word-list Creation Using CasualConc
AACL 2018 - Going Beyond Simple Word-list Creation Using CasualConc
 
Language design and translation issues
Language design and translation issuesLanguage design and translation issues
Language design and translation issues
 
External domain-specific languages
External domain-specific languagesExternal domain-specific languages
External domain-specific languages
 
Introduction to programing languages part 3
Introduction to programing languages part 3Introduction to programing languages part 3
Introduction to programing languages part 3
 
Introduction to programing languages part 2
Introduction to programing languages   part 2Introduction to programing languages   part 2
Introduction to programing languages part 2
 
Translation memory
Translation memoryTranslation memory
Translation memory
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data Types
 
C language
C languageC language
C language
 
XML | Computer Science
XML | Computer ScienceXML | Computer Science
XML | Computer Science
 
Projectional editing
Projectional editingProjectional editing
Projectional editing
 

Similar to Using Variables in Programming

Variables And Data Types
Variables And Data TypesVariables And Data Types
Variables And Data Types
aNumak & Company
 
Unit 1
Unit 1Unit 1
Cp 111 lecture 3
Cp 111 lecture 3Cp 111 lecture 3
Cp 111 lecture 3
HafidhyMasoud
 
Computer programing 111 lecture 3
Computer programing 111 lecture 3Computer programing 111 lecture 3
Computer programing 111 lecture 3
ITNet
 
Data
DataData
Data
aqib1122
 
Programming concepts By ZAK
Programming concepts By ZAKProgramming concepts By ZAK
Programming concepts By ZAK
Tabsheer Hasan
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
ZcelTablizo3
 
PCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxPCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docx
prakashvs7
 
Ndu06 typesof language
Ndu06 typesof languageNdu06 typesof language
Ndu06 typesof language
nicky_walters
 
Programming
ProgrammingProgramming
Programming
shalsmart12
 
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...
ijpla
 
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
Waqas Tariq
 
LectureNotes-01-DSA
LectureNotes-01-DSALectureNotes-01-DSA
LectureNotes-01-DSA
Haitham El-Ghareeb
 
Introduction to Visual Basic
Introduction to Visual Basic Introduction to Visual Basic
Typeful programming
Typeful programmingTypeful programming
Typeful programming
mustafa sarac
 
Htmll
HtmllHtmll
Htmll
diego
 
Htmll
HtmllHtmll
Htmll
diego
 
eMedRec Notes - design basics
eMedRec Notes - design basicseMedRec Notes - design basics
eMedRec Notes - design basics
Laura Holt
 
Ppt about programming in methodology
Ppt about programming in methodology Ppt about programming in methodology
Ppt about programming in methodology
Vaishnavirakshe2
 
Programming languages ms harsha
Programming languages ms harshaProgramming languages ms harsha
Programming languages ms harsha
Harsha Batra
 

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
 
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
 
Data
DataData
Data
 
Programming concepts By ZAK
Programming concepts By ZAKProgramming concepts By ZAK
Programming concepts By ZAK
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
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...
 
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
 
LectureNotes-01-DSA
LectureNotes-01-DSALectureNotes-01-DSA
LectureNotes-01-DSA
 
Introduction to Visual Basic
Introduction to Visual Basic Introduction to Visual Basic
Introduction to Visual Basic
 
Typeful programming
Typeful programmingTypeful programming
Typeful programming
 
Htmll
HtmllHtmll
Htmll
 
Htmll
HtmllHtmll
Htmll
 
eMedRec Notes - design basics
eMedRec Notes - design basicseMedRec Notes - design basics
eMedRec Notes - design basics
 
Ppt about programming in methodology
Ppt about programming in methodology Ppt about programming in methodology
Ppt about programming in methodology
 
Programming languages ms harsha
Programming languages ms harshaProgramming languages ms harsha
Programming languages ms harsha
 

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. 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.
  • 2. 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!