HTML and CSS Reference
In-Depth Information
Functions can be written to accept multiple arguments. Let's look at another function:
<script language=”JavaScript”>
function writeTag(tag, contents) {
document.write(“<” + tag + “>” + contents + “</” + tag + “>”);
}
</script>
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 spec-
ify the return value of the function. You can use the value returned by a function in a
conditional statement, assign it to a variable, or pass it to another function. Here's a func-
tion with are 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 values are objects.
Here's one more example, and the results are shown in Figure 14.3:
Input
<script language=”JavaScript”>
function modifyValue(myValue) {
document.write(myValue + “<br />”);
myValue = “new value”;
document.write(myValue + “<br />”);
}
var value = “old value”;
modifyValue(value);
document.write(myValue + “<br />”);
</script>
14
Search WWH ::




Custom Search