HTML and CSS Reference
In-Depth Information
Arguments are received
as parameters by the
receiving function and
stored in local variables;
they will disappear
when the function ends.
Arguments are
passed to the
receiving function.
Calling Function
Receiving Function
Figure 7.3 In the analogy of the pocket calculator, you are the caller when you
press the buttons, and the internal functions inside the calculator are the receiver.
EXAMPLE 7.2
<html>
<head><title>Passing Arguments</title>
<script type="text/javascript">
1
function greetings(pal) { / "Birdman!" is stored in pal
2
alert("Greetings to you, " + pal );
}
</script>
</head>
<body background="birdman.jpg">
3
<script type="text/javascript">
4
greetings("Birdman!"); // Passing an argument
</script>
</body>
</html>
EXPLANATION
1
The function, greetings() , has one parameter, called pal . This parameter holds a value
that is sent to the function when it was called. The parameter name is any valid Java-
Script variable name. At this point, the function has been defined but not called.
2
The alert method will display the string, “Greetings to you, ” concatenated to the
value stored in pal ; in this example that value is “Birdman!” .
3
The JavaScript program is in the body of the document. It contains a function call
that will invoke a function defined in the head of the document.
4
The function greetings() is called with one argument, “Birdman!” . This argument
will be sent to the function, and assigned to the parameter, pal . If the function had
been called in the head of the document as it was in Example 7.1, the background
image would not appear until after the user clicks the OK button in the alert box
(see Figure 7.4), but in this example, the image was loaded before the function
was called.
Search WWH ::




Custom Search