http://fishcaro.crosswinds.net/day_J6_functions.htm
INDEX CARD #J6:
FUNCTIONS (J6a)The code for a typical function is given below, that illustrates some basic features of JavaScript functions:
function square(x) This function could be written more compactly, on one line, like this: function square(x) { return x*x }
|
DEFINING A FUNCTION (J6b)the function keyword, followed by:
return statement causes the function to stop executing and return the
value of its expression (if any) to the caller.
Two sample functions are given in the following cards, together with
typical "calls" to these functions.
|
A TYPICAL FUNCTION (J6c)
<SCRIPT LANGUAGE="JavaScript">
Here's a typical call to the function (which must occur inside the SCRIPT container):
add(1,2);
which produces this:
The sum of 1 and 2 is 3.
|
A SECOND FUNCTION EXAMPLE (J6d)
Here's a typical call to the function (which must occur inside the SCRIPT container):
dothis("cat"); which produces this:
The length of cat is 3.
|
Printable version of Index Card J6a
Printable version of Index Card J6b
Printable version of Index Card J6c
Printable version of Index Card J6d
WORKSHEET #J6:
SCRIPT
container, and play with each function. Make some
simple changes. Make lots of different calls to the function.
Be sure that you understand what EVERY LINE of the code is doing before you tackle
the next part of the assignment (where you'll write your OWN functions). cube
. Have the
function print out things like this: 23 = 8
and (-2)3 = -8
.
x = 5;
var x = 5;
var x = 5%2; document.write("the value of x is ",x,"<BR>");
A | not A |
T | F |
F | T |
A | B | A AND B | A OR B |
T | T | T | T |
T | F | F | T |
F | T | F | T |
F | F | F | F |
var x = true&&true; document.write("the value of x is ",x,"<BR>");