Functions

Unit 6

This sample looks like sample 5-2-functions. The difference is that the functions have been moved to one JavaScript file, which is in the <head> and the function calls are in a separate JavaScript file in the <body>

The JavaScript file sample-6-1-functions-in-head.js contains:

function sayHello()
  {
  alert("hello");
  }

function saySomething(it)
  {
  // use an empty string if it is undefined
  var aString = it == undefined ? "" : it ;  
  alert("The word is: " + aString);
  }
          

The JavaScript file sample-6-1-in-body.js contains:

sayHello();
saySomething("What's up Doc?");
saySomething();
          

This is a good organization with the functions in the head and the code that builds the body content in the body.

Three blank lines

This sample is similar to sample 6-1.

The JavaScript file sample-6-2-functions-in-head.js contains:

function threeBlankLines()
  {
  document.write("<br /> <br /> <br />");
  }
          

The JavaScript file sample-6-2-in-body.js contains:

threeBlankLines();
          

This is a good organization with the functions in the head and the code that builds the body content in the body.

Return value

This sample shows the use of a return value.

The JavaScript file sample-6-3-functions-in-head.js contains:

function twice(value)
  {
  return 2 * value;
  }
          

The JavaScript file sample-6-3-in-body.js contains:

var result;
var start = 4;
result = twice(start);
alert(result);			// displays 8
          

Reading assignment

Reading assignments are in the text book, Java Script, A Beginner's Guide, Second Edition, by John Pollock; McGraw Hill / Osborne, ISBN 0-07-222790-7

Read Module 4.

Much of this chapter is review of programming fundamentals.
Sections 4.1 uses mathematical notation; skim this section
Sections 4.2 - Notice the use of the keyword function.
I would like it if you used brackets as shown on page 68, rather than as shown at the top of page 69.
Notice that the parameters are specified without any type; like variables, parameters do not have a type in JavaScript.
Notice that, if there is a return, it should return from the last statement in the function. The return does not have any type specified either.
Section 4.3 - Read this section carefully.
The function definition should be before the function call.
Most functions are defined within the head element, and are called from within the body element.
Global variables should be avoided, when possible. We may see some cases where globals are needed.
Section 4.4 - Look through this section, and read anything that is unfamiliar.

Alternate reading assignments are in the text book, Java Script Concepts & Techniques Programming Interactive Web Sites, by Tina Spain McDuffie; Franklin, Beedle & Associates, ISBN 1-887902-45-7

Read Chapter 8, from the beginning through Variable Scope (Global vs. Local)on page 233. We will read the rest of the chapter later.

Lecture notes

Do NOT read the lecture notes before hearing the lecture. If you do, you will find the lecture very boring. Read the lecture notes if you do not attend the lecture, or if you wish to review the material.