Docsity

JavaScript Functions & Variable Scope: Understanding Variables & Arrays, Slides of Introduction to Computing

Partial preview of the text

Download JavaScript Functions & Variable Scope: Understanding Variables & Arrays and more Slides Introduction to Computing in PDF only on Docsity!

CS101 Introduction to Computing

Lecture 29

Functions & Variable Scope

(Web Development Lecture 10)

During the last lecture we had a discussion on Arrays

  • We found out why we need arrays
  • We became able to use arrays in conjunction with the ‘for’ loop for solving simple problems

Array

4

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 can even have an array containing other arrays (^) Docsity.com^5

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

words = new Array ( 10 ) ; From the last lecture …

for ( k = 0 ; k < words.length ; k = k + 1 ) { words[ k ] = window.prompt( "Enter word # " + k, "" ) ; }

document.write( "UNSORTED WORDS:" + "
" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "
" ) ; }

words.sort( ) ;

document.write( "SORTED WORDS:" + "
" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "
" ) ; } (^) Docsity.com 8

function writeList( heading, words ) {

document.write( heading + "
" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "
" ) ; } }

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 ) ; 10

The function is defined here

The function is called here and in the next box

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

function writeList( heading, words ) { document.write( heading + "
" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "
" ) ; } }

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 ) ; (^13)

function writeList( heading, words ) {

document.write( heading + "
" ) ;

for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "
" ) ; } } 14

Keyword

Function identifier

Pair of parenthesis

Function ‘arguments’ separated by commas

Function definition enclosed in a pair of curly braces

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)

To ensure that a function is defined before it is called up, define all functions in the HEAD

portion of Web pages

19

function popUp( message ) {

window.alert( message ) ;

}

popUp( “Warning!” ) ;

function popUp( message ) { window.alert( message ) ; } a = popUp( “Warning!” ) ; document.write( a ) ;

What will get written by this statement?

undefined

function add( a, b ) {

c = a + b ; return c ;

}

sum = add( 2, 4 ) ;

document.write( sum ) ;

20

function add( a, b ) { c = a + b ; return c ; } add( 2, 4 ) ;

What would this statement do?

function add( a, b ) { c = a + b ; return c ; } document.write( add( 2, 4 ) ) ;

What would this modifica- tion do?