Creating Basic Java Script Functions
By Stephen Bucaro
A function is a group of programming statements and⁄or structures that are not performed
until you call the function by its name, which is called "executing" the function. From the outside,
you can view a function as a "black box". You put something in one end of the black box (called
"passing it to the function"), and something comes out the other end of the black box (called
"returned by the function").
Inside the black box, the function was performed. But many times a programmer doesn't
know or doesn't even care what statements and⁄or structures are inside the black box,
as long as the box works properly every time.
Java Script has many built-in functions, for example the eval() function. You pass
a mathematical expression as a string (computerese for a sentence inside quotes) to the eval()
function, and it returns the result of the expression. For example, the statement below passes
the string "2 * 6" to the eval() function and it returns the number 12, placing it in a variable
named "result".
var result = eval("2 * 6");
Java Script has hundreds of built-in functions and before you create your own function, it
would be wise to see if there is a built-in function you can use instead. If you need to create
your own function (called "declaring a function" or creating a function "declaration"), the syntax
for creating a function is shown below.
function function_name ([parameter], ... ,[parameter])
{
// statements and/or structures
return;
}
Start your function declaration with the keyword "function", then follow that with the
function's name, followed by parenthesis (), followed by curly braces {}. The function's name
must NOT be a Java Script keyword (or "reserved word"). Within the parenthesis you place
the names of one or more values (or "parameters"), separated by commas, that you want
to pass to the function. Within the curly braces you place the code that actually performs
the operations on the parameters.
Note that the last statement in the code is "return". This is actually optional because unless
an error occurs within the function code, the function IS going to return. Return means "return
to the main programs flow structure". Return can also mean "return a value". Let's create a
few basic example functions.
If you want to follow along, open Windows Notepad (Start | All Programs | Accessories | Notepad),
or your favorite ASCII text editor (Don't use a fancy word processing application like Microsoft
Word, because they add loads of formatting tags). Type in the text shown below and then
save the file with the extension .htm.
<script type="text/javascript">
alert("Hello World");
</script>
Lets call this example1.htm. Then double-click on the file and it should open in your default
web browser and a message box should appear displaying the message "Hello World". If that
worked for you, congratulations, you just created your first Java Script program. If it didn't
work, make sure you're not using a word processing application and check for typos. You might
want to drag a shortcut to Notepad to your desktop for use with future examples.
|