Chapter 10: JavaScript Functions - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Chapter 10: JavaScript Functions

Description:

... in JavaScript is ... Functions enable the principle of software reuse and help to ... script type = 'text/javascript' document.writeln( ' h1 Square numbers ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 8
Provided by: raj11
Category:

less

Transcript and Presenter's Notes

Title: Chapter 10: JavaScript Functions


1
Chapter 10 JavaScript Functions
  • CIS 275Web Application Development for Business I

2
Program Modules in JavaScript
  • Software development is best achieved in smaller
    pieces called _________, applying the principle
    of divide and conquer.
  • A module in JavaScript is called a _________.
  • A pre-defined function that belongs to a class is
    called a method (like Math.pow or Math.round).
  • A function you write is called a
    ____________-defined function.
  • A function is invoked by a function call.
  • A function call may include arguments.
  • A called function may _______ data to the caller.

3
Programmer-Defined Functions
  • All variables declared in a function are _______.
  • A function declaration may include parameters,
    which are also local variables.
  • Functions enable the principle of software reuse
    and help to avoid code repetition.
  • Format of a function definition
  • function function-name ( parameter-list )
  • declarations and statements
  • The ___ is the function call operator.
  • The first line is the function declaration and
    the content within the s is the function
    ______.

()
4
Fig. 10.2
  • lthtml xmlns "http//www.w3.org/1999/xhtml"gt
  • ltheadgt
  • lttitlegtA Programmer-Defined
    Functionlt/titlegt
  • ltscript type "text/javascript"gt
  • document.writeln(
  • "lth1gtSquare numbers from 1 to
    10lt/h1gt" )
  • for ( var x 1 x lt 10 x )
  • document.writeln( "The square of "
    x
  • " is " square( x ) "ltbr /gt" )
  • function square( y ) // do NOT use var
    here
  • return y y
  • lt/scriptgt
  • lt/headgt
  • ltbodygtlt/bodygt
  • lt/htmlgt

5
Example of a Function Call
  • lthtmlgt
  • ltheadgt
  • ltscript type"text/javascript"gt
  • function myFunction()
  • alert("HELLO")
  • lt/scriptgt
  • lt/headgt
  • ltbodygt
  • ltformgt
  • ltinput type "button" onclick "myFunction()"
  • value "Call function" gt
  • lt/formgt
  • ltpgtBy pressing the button, a function will be
    called. The function will alert a message.lt/pgt
  • lt/bodygt
  • lt/htmlgt

6
Function Call w/ Arguments and Return
  • lt?xml version "1.0"?gt
  • lt!DOCTYPE html
  • PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  • "http//www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
    "gt
  • lthtmlgt
  • ltheadgt
  • ltscript type"text/javascript"gt
  • function total(numberA, numberB)
  • return numberA numberB
  • lt/scriptgt
  • lt/headgt
  • ltbodygt
  • ltscript type"text/javascript"gt
  • document.write( total(2,3) )
  • lt/scriptgt
  • lt/bodygt
  • lt/htmlgt

7
10.8 Scope Rules
  • The scope of an identifier for a variable or
    function is the part of the program in which the
    identifier can be referenced.
  • Variables declared in the ______ element are
    global variables and have global scope.
  • Variables declared inside a function have ______
    scope and can be used only in that function.
  • Function parameters also have local scope.
  • A variable with local scope will ______ a
    variable with the same name that has global scope.
Write a Comment
User Comments (0)
About PowerShow.com