ptg16476052
The JavaScript Language 487
17
Functions can be writ ten to accept multiple arguments. Let’s look at another function:
function writeTag(tag, contents) {
document.write("<" + tag + ">" + contents + "</" + tag + ">");
}
This function accepts two arguments: a tag name and the contents of that tag. There’s one
special statement that’s specific to functions: the return statement. It is used to specify
the return value of the function. You can use the value returned by a function in a condi-
tional statement, assign it to a variable, or pass it to another function. Here’s a function
with a return value:
function addThese(value1, value2) {
return value1 + value2;
}
Here are a couple of examples of how you might use that function:
if (addThese(1, 2) > 10) {
document.write("Sum is greater than 10.");
}
var sum = addThese(1, 2);
One other thing to note is that the values passed to function as arguments are copies of
those values unless the arguments are objects.
Here’s one more examp le, and the results are shown in Figure 17.6:
Input ▼