Notes
Slide Show
Outline
1
Functions
  • Chapter 7 of Reed
  • Abstraction and User-Defined Functions
2
Abstraction
  • abstraction is the process of ignoring minutiae and focusing on the big picture
    • in modern life, we are constantly confronted with complexity
    • we don't necessarily know how it works, but we know how to use it


    • e.g., how does a TV work?  a car?  a computer?
3
User-defined Functions
  • JavaScript’s predefined functions represent a collection of useful, general-purpose abstractions
    • the programmer can add additional abstractions via user-defined functions
    • once defined, a user-defined function can be used the same way as a predefined function


    • e.g., consider converting a temperature from Fahrenheit to Celsius
      • tempInCelsius = (5/9) * (tempInFahr - 32);
4
Dissecting the Function
  • the 1st line specifies that we are defining a function named FahrToCelsius that takes one input
    • the variable name in parentheses is known as a parameter
    • when the function is called, the provided input is assigned to the parameter
      • e.g., for the call FahrToCelsius(32), the value 32 would be assigned to tempInFahr for the calculation
5
General Function Form
  • in general, the form of a user-defined function is as follows:









    • function names follow the same rules as variables: consist of letters, digits, and underscores and must start with a letter
    • note that there may be:
      • more than one parameter, separated by commas
      • no parameters at all, in which case the function name is followed by ()
      • more than one statement inside the curly braces ,separated by semicolons
6
Programmer defined function
  • define the function in the HEAD so that it’ll be available when it’s called.


  • function is not executed till it is called.


  • once the function is defined in the HEAD, it can be called just like a predefined function.


  • to include a user-defined function in a page, the simplest way is to include its definition in SCRIPT tags in the HEAD so it will be ready for use.


7
Temperature Conversion Page
8
Function without Return
9
Function without Return
  • here is a function that displays a verse of the song "Old MacDonald had a Farm"
    • the function has two parameters for the animal name and sound
    • by calling the function with different inputs, you can display different verses
  • how much work is it to
  • rearrange the verses?
  • add a new verse?
  • change all the verses?
10
Old MacDonald
  • each call to the OldMacVerse function displays the verse with the specified animal & sound


    • to rearrange the verses, simply rearrange the calls in the BODY


    • to add a new verse, simply add a new call in the BODY


    • to make a change to all verses, (e.g., change indentation) simply make the change once in the function definition
11
Multiple Inputs
  • if a function has more than one input,
    • parameters in the function definition are separated by commas
    • input values in the function call are separated by commas


    • values are matched to parameters by order
      • 1st input value in the function call is assigned to the 1st parameter in the function
      • 2nd input value in the function call is assigned to the 2nd parameter in the function
      • . . .


12
Designing Functions
  • functions do not add any computational power to the language
    • a function definition simply encapsulates other statements

  • still, the capacity to define and use functions is key to solving complex problems, as well as to developing reusable code
    • encapsulating repetitive tasks can shorten and simplify code
    • functions provide units of computational abstraction – user can ignore details
    • functions are self-contained, so can easily be reused in different applications




13
Parameters
  • parameters play an important role in functions
  • they facilitate the creation of generalized computations
  • the function defines a formula, but certain values within the formula can differ each time the function is called


14
Dynamic Images
  • just as you can use user-initiated events to change the contents of
  • text boxes, you can also dynamically modify images


  • <img name="photo" src="happy.gif" alt="Happy Face" />


    • causes the image stored in the file happy.gif to appear in the page

  • you can change the image by reassigning its SRC attribute in a script.
    • similar to the way that text boxes have their VALUE attribute reassigned
    • absolute name of an image: document.images.IMAGE_NAME.src


  • document.images.photo.src = "sad.gif";


    • replaces happy.gif with sad.gif



  • note: images do not need to be embedded in forms
15
Dynamic Image Example
  • initially the image is set to happy.gif


  • when a button is clicked, the ChangeImage function is called to change the image


  • the new image is specified by the parameter to the function
16
Dynamic Image Example