Changes

Jump to: navigation, search

Understanding JavaScript Functions

868 bytes added, 18:41, 24 April 2007
Passing Arguments to a Function
<pre>
 
<html>
<head>
<title>A Simple JavaScript Function Example</title>
<script language="JavaScript" type="text/javascript">
 
function sayHello()
{
alert("Hello!");
}
</script>
</head>
<h1>Calling a function with arguments</h1>
 
<script language="JavaScript" type="text/javascript">
sayHello( "24th", "July" );
sayHello ( "1st", "August" );
sayHello ( "24th", "May" );
</script>
 
</body>
</html>
 
</pre>
 
In this example the function is called multiple times, passing through arguments that are then used in the function to construct a string to be displayed in the alert dialog. To achieve this without functions it would ne necessary to repeat the script in the function 3 times over in the body. Clearly placing the script ina function and re-using it over and over is a much more efficient approach.
 
Next we need to look at how a function might return a value.
<h1>Calling a function</h1>

Navigation menu