Professional Documents
Culture Documents
Lecture 29
Functions & Variable Scope (Web Development Lecture 10)
1
Array
An indexed list of elements A variable is a container that holds a value Similarly, an Array can be considered a container as well, but this one is more interesting as it can hold multiple values
Array
fruit[ 0 ]
Identifier
Square bracket
Index
Arrays in JavaScript
In JavaScript, arrays are implemented in the form of the Array object The key property of the Array object is length, i.e the number of elements in an array Two of the key Array methods are:
reverse( ) sort( )
Elements of an array can be of any type; you 5 can even have an array containing other arrays
Function
A group of statements that is put together (or defined) once and then can be used (by reference) repeatedly on a Web page Also known as subprogram, procedure, subroutine
for ( k = 0 ; k < words.length ; k = k + 1 ) { words[ k ] = window.prompt( "Enter word # " + k, "" ) ; } document.write( "UNSORTED WORDS:" + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; } words.sort( ) ; document.write( "SORTED WORDS:" + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; 8 }
for ( k = 0 ; k < words.length ; k = k + 1 ) { words[ k ] = window.prompt( "Enter word # " + k, "" ) ; } document.write( "UNSORTED WORDS:" + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; } words.sort( ) ; document.write( "SORTED WORDS:" + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; 9 }
function writeList( heading, words ) { document.write( heading + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; } The function is } called here and words = new Array ( 10 ) ; in the next box The function is for ( k = 0 ; k < words.length ; k = k + 1 ) { defined here
words[ k ] = window.prompt( "Enter word # " + k, "" ) ; }
10
Advantages of Functions
Number of lines of code is reduced Code becomes easier to read & understand
Code becomes easier to maintain as changes need to be made only at a single location instead multiple locations
11
function writeList( heading, words ) { document.write( heading + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; } }
words = new Array ( 10 ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { words[ k ] = window.prompt( "Enter word # " + k, "" ) ; }
function writeList( heading, words ) { document.write( heading + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; } }
words = new Array ( 10 ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { words[ k ] = window.prompt( "Enter word # " + k, "" ) ; }
Pair of parenthesis Function arguments separated by commas Function definition enclosed in a pair of curly braces
Function Identifiers
The naming rules for function identifiers are the same as were discussed for variable and array identifiers
15
Arguments of a Function
A comma-separated list of data Arguments define the interface between the function and the rest of the Web page Arguments values are passed to the function by value (some popular languages pass arguments by reference as well)
16
To ensure that a function is defined before it is called up, define all functions in the HEAD portion of Web pages
17
undefined
function factorial( n ) { Another Example product = 1 ; for ( k = 1 ; k <= n ; k = k + 1 ) { product = product * k } return product ; }
5! = 120
0! = ?
21
recursive
use of a function
22
function method
23
Methods
Methods are functions They are unusual in the sense that they are stored as properties of objects
24
prop 1 prop 2
prop 3
prop 4
prop 5
method 3
25
method 1
prop 3
prop 4
prop 5
prop 7
26
Event Handlers
Special-purpose functions that come predefined with JavaScript They are unusual in the sense that they are many times called in the HTML part of a Web page and not the <SCRIPT> </SCRIPT> part
parseInt( )
Syntax: parseInt ( string ) string1 = 3.14159 ; document.write( parseInt( string1 ) ) ; document.write( <BR> ) ; string2 = $100.00 ; document.write( parseInt( string2 ) ) ; document.write( <BR> ) ; string3 = 23 ; document.write( parseInt( string3 ) ) ;
3 NaN 23
30
parseInt( )
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
31
parseInt( )
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
32
parseFloat( )
Syntax: parseFloat ( string ) string1 = 3.14159 ; document.write( parseFloat( string1 ) ) ; document.write( <BR> ) ; string2 = $100.00 ; 3.14159 document.write( parseFloat( string2 ) ) ;NaN document.write( <BR> ) ; 2.3E-14 string3 = 23E-15 ; document.write( parseFloat( string3 ) ) ; 33
parseFloat( )
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 34
parseFloat( )
3. If the first character cannot be converted to a number, parseFloat returns NaN 4. Leading and trailing spaces are ignored
35
Scope of Variable
Defining the space in which a variable is effective is known as defining the scope of a variable
36
37
function factorial( n ) { Example product = 1 ; for ( k = 1 ; k <= n ; k = k + 1 ) { product = product * k } return product ; } n = window.prompt( "Enter a number ", "" ) ;
39
40
function factorial( n ) { product = 1 ; for ( k = 1 ; k <= n ; k = k + 1 ) { product = product * k } return product ; } n = window.prompt( "Enter a number ", "" ) ;
function factorial( n ) { product = 1 ; for ( k = 1 ; k <= n ; k = k + 1 ) { product = product * k } 10! = 3628800 return product ; k = 11 } n = window.prompt( "Enter a number ", "" ) ;
42
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 ", "" ) ;
44
45
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 is local to the factorial function, and does not hold any meaning outside that function
46
Here product has function factorial( n ) { 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 ; }
document.write(n, "! = ", factorial( n ) ) ; document.write( <BR> ) ; What would this document.write( product ) ; statement write?
47
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
48
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) 49
var a, b ; p=q+2;
r=s;
var u ; document.write( m ) ; var c, d ; x=y*y;
Global Local u a m b p c q d x y r s
50
function writeList( heading, words ) { document.write( heading + "<BR>" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ; } Would the functionality 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, "" ) ; }
53
Assignment #10
Develop a Web page that includes functions that determine the mean and median of a set of numbers stored in an array Make sure to declare all variables that you use in the main code as well as the functions
Further information on this assignment will be provide on the CS101 Web site
55
56