JavaScript Chapter 7




Skip to Chapter   1  2  3  4  5  6  7  8  9  10 

                                                              Chapter 7 -- Functions






What is a Function?

A function is a piece of code that runs when you call it.

Here is an example of a function:
<script language="JavaScript">
function ValidateUser()
{
password = prompt("Enter Password","test");
if (password == "test")
{
alert('You Got It!!!');
}
else
{
alert('Did you eat paint chips as a child?');
}
}
</script>
In order to call it you can just do this anywhere in your code:
ValidateUser();
You're probably asking "What is the use in using functions?! Why not just type out the code you need instead of making a 'function' and then 'calling' it to have the same result."
Here are just a few reasons to use functions in your code:
It may be easy to read your code right after you finish typing it, but when you come back to it later (and you inevitably will), your script will look like ancient Greek text.
Another reason for making functions is to use the same process in your code without having to retype it.
Yet still another reason for using functions is to make your own custom commands that you can use in later programs (I'll show you how to insert your commonly used scripts/functions later).
There is but still one more reason for using functions I can think of; if/when you are working in any compitent corporation you will be working on a team. It is important for your team members to be able to reffer to a function and not lines 298 to 304.
Here is an example of a function (all be it useless):

Play around with this code and reopen the page if you accidentally mess up and can't fix it.

Functions in Depth

The identifier function tells the computer that the next word is going to be the name of a function you made up.
The two parenthesis after the function normally have variables in them that are being given specifically to that function.
An example of a function that has these variables in the parenthesis is:
function functionName ( variable1 , variable2 )
{
scripting goes here
}
In order to send a variable to a function you would type:
functionName ( variableName );
You can use this concept of passing variables to functions when you want a function to have different output i.e.:
<input type="button" value="Go To BPA.org" onclick="javascript:goToAddress('http:\\www.bpa.org');">
<input type="button" value="Go To PBS.org" onclick="javascript:goToAddress('http:\\www.pbs.org');">
<input type="button" value="Go To perl.com" onclick="javascript:goToAddress('http:\\www.perl.org');">
<input type="button" value="Go To The Moon" onclick="javascript:goToAddress('http:\\www.the MMMMMMMOOOOOOOOOOOOOOOOOOOONNNNN!!!');">

<script language="Javascript">
function goToAddress(address)
{
window.location.href = address;
}
</script>
Just three more to go!
Here are some of the things you'll need to remember about functions:
USE FUNCTIONS WHENEVER YOU CAN!
Functions make your code easier for you to understand your code when you come back to it later.
The use of functions acually speeds up your larger programs because the computer doesn't have to hunt through your code either.
The eighth chapter will go over the hierarchy of javascript.
Forms Functions Hierarchy