Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
1099
JavaScript Functions
Introduction
Functions are an important part in any programming language.
A function is a block of commands, which can be executed again and again, by
the calling the name. JavaScript contains some inbuilt functions, which can be
used freely and you can create your own functions. A function contains of a name and a body. Each function starts
with the keyword function and then the name of the function. After that
you can put your required commands in the body part which must be enclosed with
the {} brackets. Once you have created your function you can call it with the
name of the function. The following examples demonstrate this.
The above example displays a simple function, however you
can also pass arguments to a function. For that you have to define the
parameter in the in the brackets of the function name. Once you have declared
your parameter you can simply pass the value when calling the function. The
following example demonstrates it.
Example
…
function CalculcatePI(number)
{
document.write("This
is a function with parameters.<br/>")
var PI =
3.14
document.write(number*PI+"<br/>")
}
// Call function and pass 5 as a parameter
CalculcatePI(5)
…
Output
JavaScript Functions with Return Values
Functions can also return the value instead of displaying
it. This can be useful if you need to do some more processing with the variable
returned by the function. The following example demonstrates it.
Example
…
function ReturnPIValue(number)
{
document.write("This
is a function with parameters and a return value.<br/>")