Tuesday 7, September 2010
      Home       Tutorials       Free Ebooks       Free Scripts       Tech News       Articles       About Us

Functions

A function contains code that will be executed by an event or by a call to that function. You can however use a function to contain the script and call it every time your need it. Here is the syntax for a function.

Syntax:
 function name (parameters){
   statements

   }
The function keyword identifies this as a function. The parameters in the parenthesis ( ) provide a means of passing values to the function. There can be as many parameters separated by commas as you need. It is perfectly ok to have a function with no parameters. These parameters are variables which are used by the JavaScript statements inside the curly braces { }. The var keyword is not needed to declare the parameters in a function as variables because they are automatically declared and initialized when the function is called. The function can return a value by using the return keyword.

You should put your functions in the HEAD section of your document. Functions will work if you put them in the BODY section. However, a function must be loaded prior to the statement that calls it. Putting all functions in the HEAD section is the way to insure this.

Example:
 function myAdder (num1, num2){
   var total = num1 + num2
   document.write(total)
   }

You would place this function inside some tags in the head section. You then can call the function from anywhere in your document. In this example, lets call it from within some tags in the body of document like this.

 myAdder(23, 56)

Scope of Variables
A variable that is defined outside of a function is a global variable. A variable that is defined inside of a function is a local variable. What does this mean? Global variables can be used anywhere in the document that is currently loaded in the browser. They can be declared in the HEAD section and used in any function or even in the BODY section. Local variables can only be used in the function that declares them. Yes, you could leave the var keyword off of the variables you declare inside of a function and they will then be global. I do not recommend this.

   
 
JavaScript Tutorial
 
  Introduction
Introduction
  Object Oriented Programming
Object Oriented Programming
  How to
How to
  Where use JavaScript
Where use JavaScript
  Variables
Variables
  Operators
Operators
  if Statement
if Statement
  Switch Statement
Switch Statement
  Functions
Functions
  for Loop
for Loop
  while Loop
while Loop
  Popup Boxes
Popup Boxes
  break and continue Statements
break and continue Statements
  Events
Events
  Date and Time Object
Date and Time Object
  Special Characters
Special Characters
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download Mammoth Myspace Secrets Ebook Mammoth Myspace Secrets
   
Download Road Pricing  Ebook Road Pricing
   
Download Special Report  Ebook Special Report
   
Download Developing Web Applications with Ant  Ebook Developing Web Applications with Ant
   
Download Prospecting and the Sales Pitch Ebook Prospecting and the Sales Pitch
   
Technology News      
Studiesinn.com © 2010 All Rights Reserved.