function someFuncName(arg1, arg2) {
first line to be executed;
second line to be executed;
any JavaScript statements go here;
etc. ;
}
document.write("write this!");
alert("here comes the function call!");
someFuncName("cat", 2);
alert("I just called the function in the previous line!");
document.write() method requires a parameter, the string that you want written to the screen). Let's look at a working function to make this clearer.
<html>
<head>
<script type="JavaScript">
<!--
function findSeconds(name,age)
{
var secs = age * 365 * 24 * 60 * 60;
document.write ("Hi "+name+"! You have lived " + secs + " seconds! ");
}
// -->
</script>
</head>
<body>
<script type="JavaScript">
<!--
var userName = prompt ( "Please enter name : ", "" );
var userAge = prompt ( "Please enter your age : ", "" );
findSeconds(userName,userAge);
// -->
</script>
</body>
</html>
see the code in action
In the example above, we defined the function findSeconds() in the HEAD portion of the document. In the definition, we indicated that findSeconds() requires 2 pieces of information, name and age. You can think of name and age as being variables created for findSeconds() to use. When you call findSeconds(), you provide values for findSeconds() put into name and age. I could also have called findSeconds() this way:
findSeconds("joe", 25)
but instead I created another set of variables used to hold the user's response, and then passed the values contained in those variables into the function. Confused? Let me know at this point in the lecture!
Here's another example:
<html>
<head>
<script type="JavaScript">
<!--
function findTotal(first,next,last)
{
var total = first + next + last;
document.write ("Your total is " + total + ". ");
}
// -->
</script>
</head>
<body>
<script type="JavaScript">
<!--
findTotal(2,3,4);
findTotal(1,5,8);
findTotal(6,8,2);
// -->
</script>
</body>
</html>
see the code in action
findTotal() this way instead:
<html>
<head>
<script type="JavaScript">
<!--
function findTotal(first,next,last)
{
var total = first + next + last;
return total;
}
// -->
</script>
</head>
<body>
<script type="JavaScript">
<!--
total1 = findTotal(2,3,4);
document.write ("Your total is " + total1 + ". ");
total2 = findTotal(1,5,8);
document.write ("Your total is " + total2 + ". ");
total3 = findTotal(6,8,2);
document.write ("Your total is " + total3 + ". ");
// -->
</script>
</body>
</html>
see the code in action
Although this example looks more complicated than the first, this way of setting up your function would be much better if you might need to do something else with total later on, such as determine if the order qualifies for shipping.
<html>
<head>
<script type="JavaScript">
<!--
function findTotal(first,next,last)
{
var total = first + next + last;
return total;
}
// -->
</script>
</head>
<body>
<script type="JavaScript">
<!--
total1 = findTotal(2,3,4);
document.write ("Your total is " + total1 + ". ");
if (total1 > 15) {
document.write("This order qualifies for free shipping!");
}
total2 = findTotal(1,5,8);
document.write ("Your total is " + total2 + ". ");
if (total2 > 15) {
document.write("This order qualifies for free shipping!");
}
total3 = findTotal(6,8,2);
document.write ("Your total is " + total3 + ". ");
if (total3 > 15) {
document.write("This order qualifies for free shipping!");
}
// -->
</script>
</body>
</html>
see the code in action
Actually, you'll notice that you keep using the same basic code over and over again for determining free shipping, which is a sure-fire sign that you should use a function instead to clean up your code. So you might have the following:
<html>
<head>
<script type="JavaScript">
<!--
function findTotal(first,next,last)
{
var total = first + next + last;
return total;
}
function freeShip(total)
{
if (total > 15) {
document.write("This order qualifies for free shipping!");
}
}
// -->
</script>
</head>
<body>
<script type="JavaScript">
<!--
total1 = findTotal(2,3,4);
document.write ("Your total is " + total1 + ". ");
freeShip(total1);
total2 = findTotal(1,5,8);
document.write ("Your total is " + total2 + ". ");
freeShip(total2);
total3 = findTotal(6,8,2);
document.write ("Your total is " + total3 + ". ");
freeShip(total3);
// -->
</script>
</body>
</html>
see the code in action
Actually, we could improve the function freeShip() even more. Right now, the function always checks if the total is greater than 15, but say the qualifying total changes from month to month, or maybe the qualifying total is different for different types of items (say $15 for cd's, but $5 for books). We could redefine freeShip() to make it more flexible this way:
<html>
<head>
<script type="JavaScript">
<!--
function findTotal(first,next,last)
{
var total = first + next + last;
return total;
}
function freeShip(total, qualify)
{
if (total > qualify) {
document.write("This order qualifies for free shipping!");
}
}
// -->
</script>
</head>
<body>
<script type="JavaScript">
<!--
total1 = findTotal(2,3,4);
document.write ("Your total is " + total1 + ". ");
freeShip(total1, 5);
total2 = findTotal(1,5,8);
document.write ("Your total is " + total2 + ". ");
freeShip(total2, 15);
total3 = findTotal(6,8,2);
document.write ("Your total is " + total3 + ". ");
freeShip(total3, 15);
// -->
</script>
</body>
</html>
see the code in action
Note that once a function returns a value, the function ends, even if there is more code following the return statement!