ZeePedia

Variables: Local and Global Variables

<< THE INTERNET
Internet Services: FTP, Telnet, Web, eMail, Instant messaging, VoIP >>
img
Introduction to Computing ­ CS101
VU
document.write( parseInt( string1 ) ) ;
3
document.write( "<BR>" ) ;
NaN
string2 = "$100.00" ;
23
document.write( parseInt( string2 ) ) ;
document.write( "<BR>" ) ;
string3 = " 23 " ;
document.write( parseInt( string3 ) ) ;
1.  Parses the string argument; returns an integer
2.  If it encounters a non-numeral - anything other than (+,-) or (0-9) - it ignores it and all succeeding
characters, and returns the integer value parsed up to that point
3.  If the first character cannot be converted to a number, parseInt returns NaN
4.  parseInt truncates numbers to integer values
5.  Leading and trailing spaces are ignored
parseFloat( )
Syntax: parseFloat ( string )
string1 = "3.14159" ;
document.write( parseFloat( string1 ) ) ;
document.write( "<BR>" ) ;
string2 = "$100.00" ;
document.write( parseFloat( string2 ) ) ;
document.write( "<BR>" ) ;
string3 = " 23E-15 " ;
document.write( parseFloat( string3 ) ) ;
1.  Parses the string argument; returns a FP number
2.  If it encounters a character other than
A sign (+,-)
A numeral (0-9)
A decimal point
An exponent
it returns the value up to that point, ignoring that and all succeeding characters
3.  If the first character cannot be converted to a number, parseFloat returns NaN
4.  Leading and trailing spaces are ignored
29.6 Scope of Variable
Defining the space in which a variable is effective is known as
defining the scope of a variable. A variable can be either local or global in scope.
Local and Global Variables
Local or Function-level Variable
Effective only in the function in which they are declared
Global Variables
Visible everywhere on the Web page
Example
function factorial( n ) {
product = 1 ;
for ( k = 1 ; k <= n ; k = k + 1 ) {
product = product * k
}
return product ;
}
n = window.prompt( "Enter a number ", "" ) ;
document.write( "k = ", k ) ;
document.write( "<BR>" ) ;
189
img
Introduction to Computing ­ CS101
VU
document.write(n, "! = ", factorial( n ) ) ;
function factorial( n ) {
product = 1 ;
for ( k = 1 ; k <= n ; k = k + 1 ) {
product = product * k
}
return product ;
}
n = window.prompt( "Enter a number ", "" ) ;
document.write( "k = ", k ) ;
document.write( "<BR>" ) ;
document.write(n, "! = ", factorial( n ) ) ;
function factorial( n ) {
product = 1 ;
10! = 3628800
for ( k = 1 ; k <= n ; k = k + 1 ) {
k = 11
product = product * k
}
return product ;
}
n = window.prompt( "Enter a number ", "" ) ;
190
img
Introduction to Computing ­ CS101
VU
document.write(n, "! = ", factorial( n ) ) ;
document.write( "<BR>" ) ;
document.write( "k = ", k ) ;
function factorial( n ) {
var k ;
product = 1 ;
for ( k = 1 ; k <= n ; k = k + 1 ) {
product = product * k
}
return product ;
}
n = window.prompt( "Enter a number ", "" ) ;
document.write(n, "! = ", factorial( n ) ) ;
document.write( "<BR>" ) ;
document.write( "k = ", k ) ;
191
img
Introduction to Computing ­ CS101
VU
`k' is a Local Variable
`k' is not declared or used in the main code
Instead, it is declared within the function `factorial' only
`k' i local to the `factorial' function, and does not hold any meaning outside that function
function factorial( n ) {
Here `product' has been made a local
var k, product ;
variable as well
product = 1 ;
for ( k = 1 ; k <= n ; k = k + 1 ) {
product = product * k
}
return product ;
}
n = window.prompt( "Enter a number ", "" ) ;
document.write(n, "! = ", factorial( n ) ) ;
document.write( "<BR>" ) ;
document.write( product ) ;
Local Variables
Declaring variables (using the var keyword) within a function, makes them local·They are available only
within the function and hold no meaning outside of it
Global Variables
All other variables used in a Web page (or window) are global
They can be manipulated from the main code as well as from any of the functions
They include:
­All variables declared in the main code
­All variables used but not declared in the main code
­All variables used but not declared in any of the functions defined on the Web page (or window)
function writeList( heading, words ) {
document.write( heading + "<BR>" ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
Would the functionality
document.write( words[ k ] + "<BR>" ) ;
change if we delete the
argument `words' from these
}
4 places?
}
words = new Array ( 10 ) ;
for ( k = 0 ; k < words.length ; k = k + 1 ) {
words[ k ] = window.prompt( "Enter word # " + k, "" ) ;
}
writeList( "Unsorted Words:", words ) ;
words.sort( ) ;
writeList( "Sorted List:", words ) ;
words.reverse( ) ;
writeList( "Reverse-Sorted List:", words ) ;
Local ­vs- Global
Global variables can make the logic of a Web page difficult to understand
Global variables also make the reuse and maintenance of your code much more difficult
192
img
Introduction to Computing ­ CS101
VU
Glob
Local
u
a
var a, b ;
m
b
p=q+2;
p
c
var u ;
q
d
document.write( m ) ;
r=s;
x
y
var c, d ;
r
x=y*y;
s
Variables declared within functions are local; all others global
HEURISTIC:
If it's possible to define a variable as local, do it!
During Today's Lecture ...
We looked at functions and their use for solving simple problems
We became familiar with a couple of JavaScript's built-in functions
We became familiar with the concept of local and global variables
Next Web Dev Lecture:
Event Handling
We'll learn to appreciate the concept of event driven programming
We will produce solutions for simple problems using various event handlers
193
Table of Contents:
  1. INTRODUCTION
  2. EVOLUTION OF COMPUTING
  3. World Wide Web, Web’s structure, genesis, its evolution
  4. Types of Computers, Components, Parts of Computers
  5. List of Parts of Computers
  6. Develop your Personal Web Page: HTML
  7. Microprocessor, Bus interface unit, Data & instruction cache memory, ALU
  8. Number systems, binary numbers, NOT, AND, OR and XOR logic operations
  9. structure of HTML tags, types of lists in web development
  10. COMPUTER SOFTWARE: Operating Systems, Device Drivers, Trialware
  11. Operating System: functions, components, types of operating systems
  12. Forms on Web pages, Components of Forms, building interactive Forms
  13. APPLICATION SOFTWARE: Scientific, engineering, graphics, Business, Productivity, Entertainment, Educational Software
  14. WORD PROCESSING: Common functions of word processors, desktop publishing
  15. Interactivity to Forms, JavaScript, server-side scripts
  16. ALGORITHMS
  17. ALGORITHMS: Pseudo code, Flowcharts
  18. JavaScript and client-side scripting, objects in JavaScript
  19. Low, High-Level, interpreted, compiled, structured & object-oriented programming languages
  20. Software Design and Development Methodologies
  21. DATA TYPES & OPERATORS
  22. SPREADSHEETS
  23. FLOW CONTROL & LOOPS
  24. DESIGN HEURISTICS. Rule of thumb learned through trial & error
  25. WEB DESIGN FOR USABILITY
  26. ARRAYS
  27. COMPUTER NETWORKS: types of networks, networking topologies and protocols
  28. THE INTERNET
  29. Variables: Local and Global Variables
  30. Internet Services: FTP, Telnet, Web, eMail, Instant messaging, VoIP
  31. DEVELOPING PRESENTATIONS: Effective Multimedia Presentations
  32. Event Handlers
  33. GRAPHICS & ANIMATION
  34. INTELLIGENT SYSTEMS: techniques for designing Artificial Intelligent Systems
  35. Mathematical Functions in JavaScript
  36. DATA MANAGEMENT
  37. DATABASE SOFTWARE: Data Security, Data Integrity, Integrity, Accessibility, DBMS
  38. String Manipulations:
  39. CYBER CRIME
  40. Social Implications of Computing
  41. IMAGES & ANIMATION
  42. THE COMPUTING PROFESSION
  43. THE FUTURE OF COMPUTING
  44. PROGRAMMING METHODOLOGY
  45. REVIEW & WRAP-UP of Introduction to Computing