Amazon.com: PC Books || Educational Software || Magazines
Amazon: Books-CA || Software-CA || Books-UK || Software-UK

HTML | Javascript | ASP | PHP | VBScript | SQL | Hardware | PC FAQ| WinXP|

Learn to Build, Upgrade, or Repair your Computer Ebook + PC Safety 101 kwwebservice.com Reasonably priced web development & hosting
JavaScript
Introduction
Variables & Arrays
Functions
If Statements
Case Statements
Loop Statements
Operators
Form Validation
Alert
Date Objects
Window Objects
Javascript Books
Books @ CA Amz
Books @ UK Amz
Books @ US Amz

  :: Home
PC Topics
  :: Build A PC
  :: Windows XP
  :: PC Help
Tutorials
  :: HTML
  :: JavaScript
  :: ASP
  :: PHP
  :: VBScript
  :: SQL
Miscellaneous
  :: Code/Scripts
  :: Forum
  :: Links
  :: Contact us
  :: Tell A Friend
  :: In Somali
  ::
Web Articles
  Top Web Hosting
Reviews
  Processing
Online Card Payment Guide
  Domain name
registration & buying guide
  Getting
a website online guide
  Search Engine
submission & optimization tips
Netfirms Web Hosting
Free trial
Quality business correspondence.
Yahoo! Search Marketing

Javascript Functions

Like any other languages, javascript can be categorized by functions. Function is a collection of javascript code that performs an specific task and can be used anywhere in the document by calling it to execute. A function can also return a value. The advantage of it, is to avoid repeating codes that does same tasks all over your document or calculate specific task. Functions are usually defined in the head of the document and called in the body.
Here is a syntax for none-parameterized function:
function functionName()
(
  java script statement
)
Here is a syntax for parameterized function:
function functionName(argument1, argument2, etc)
(
  javascript statement
)
A function can return a value. Function that returns a value must have a return statement in it. The following function returns the product of two numbers:
function product()
(
  x=2;
  y=3;
  z=x*y;
  return z;
)

Functions are not executed unless they are called to be executed. When you calling a function, three things are important.

  • The name of the function

  • The number and type of parameters the function expects

  • The return value; for example, a number, a string, or whatever

Functions are usually called from the body section of html document within a javascript code. Here is how you would call a none-parameterized function:
functionName()
And here is how you would call parameterized function:
functionName(argument, argument, etc).

The following is complete example of none-parameterized function:
<html>
<head>
<script language="javascript">
function calculate()
{
    var X = 5;
    var Y = 4;
    var Z= X*Y;
    var xval="X has the value: ";
    var yval=" Y has the value: ";
    document.write(""+xval+""+""+X+""+","+""+yval+""+""+Y+""+","+" X*Y is equal "+""+Z+"");
}
</script>
</head>
<body>
<script language="javascript">
    calculate();
</script>
</body>
</html>
A variable that has a double quoted value is called string variable because the values are kept as they appear. It's integer variables such as X & Y above that can be calculated. Document.write has an object 'document' and a method 'write'. Document defines characteristics of the overall body of a web page and write method writes the expression to the specific document object. The function is called from the body.
The result of the above sample is as follows:
X has the value: 5, Y has the value: 4, X*Y is equal 20

The following is complete example of parameterized function:
<html>
<head>
<script language="javascript">
function names(firstName,lastName)
{
    document.write (firstName);
    document.write(lastName);
    document.write("<br>");
}
</script>
</head>
<body>
<script language="javascript">
    names("Cilmi"," B.");
    names("Warsame"," Dool");
    names("H."," Faaruuq");
</script>
</body>
</html>
When arguments are passed to a function, their values must be provided when the function is called. This function simply lists the provided values on the page. We passed two arguments; firstName and lastName to the function. Then we write the arguments using document.write statement. The function expects value when it's called. Call without values will display undefined.
The following is result from this script:
Cilmi, B.
Warsame, Dool
H. Faaruuq

The following is complete example of a parameterized function that returns value:
<html>
<head>
<script language="javascript">
function calculateTotal(numOrdered, itemPrice)
{
    var total=numOrdered*itemPrice;
    return total;
}
</script>
</head>
<body>
<script language="javascript">
    var price=calculateTotal(4,5.99);
    document.write("Total: "+price);
</script>
</body>
</html>
We passed numOrder and itemPrice to this function. We created a variable called total which is equal to product of the two other numbers. Then we returned the total.
We then created another variable called price when the function is called and set it equal to the function name and argument. This variable simply stores returned value from the function. Then we write the variable price using document.write statement.
The result of this script is as follows:
Total: 23.96

Variables & Arrays If Statement
Bluehost.com Web Hosting $6.95
PC System Tools
SpyCleaner
Registry Mechanic
4Diskclean Pro
Health PC Club
TweakNow
Registry Washer
SpyAnyWhere
System Lifeguard
...More/Details

PC Articles
  Computer Safety
prevent viruses & Spyware