10. JavaScript Functions

JavaScript Functions

1.How to Define a Function
2.Javascript Function Examples
3.The return Statement
4.The Lifetime of JavaScript Variables

the function will be executed by an event or by an explicit call to the function

to keep the browser from executing a script when the page loads, you can put your script into a function
you may call a function from anywhere whithin a page ( or even from other pages if the function is embedded in an external .js file)

Functions can be defined both in the <head> and in the <body> section of a document.
However, to ensure that a function is read/loaded by the browser before it is called,
it should be placed in the <head> section.

How to define a function

function functionname(var1,var2,...varx)
{
some code
}

The parameters var1,var2, and so on, are variables or values passed into the function.
The { and the } defines the start and end of the function

A function with no parameters must include the paranthasis () after the function name

The word function must be written in lowercase letters, otherwise a javascript error occurres

JavaScript Function Example

<html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World");
}
</script>
</head>
<body>
<form>
<input type="button" value="click Me!" onClick="displaymessage()"/>
</form>
<p>By Pressing the button above, a function will be called.
the function will alert a message.</p>
</body>
</html>

<html>
<head>
<script type="text/javascript">
function myfunction(txt)
{
alert(txt);
}
</script>
</head>
<body>
<form>
<input type="button" value="call function" onClick="mufunction('Hello')">
</form>
<p>By pressing the button above, a function will be called with "Hello" as a parameter.
the function will alert th parameter.</p>
</body>
</html>

<html>
<head>
<script type="text/javascript">
function myFunction()
{
 return("Hello World");
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(myFunction())
</script>
<body>
</html>

The Return Statement

The return statement is used to specify the value that is returned from the function

A return statement also may be used in a function that does not return a value to end execution at any given point in the function;

var globalName;
function setGlobalName(name)
{
if(name.length==0)
{
Alert("no name specified")
return;
}
globalName=name;
}

<html>
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(product(4,3));
</script>
<p>The script in the body section calls a function with two parameters(4 and 3)</p>
<p>The functiopn will return the product of these two parameters.</p>
</body>
</html>

The Lifetime of JavaScript Variables

Local Variables:
If you declare a variable within a function, the variable can be accessed only within that function.
when you exit the function, the variable is destroyed.
These variables are called local variables.

you can have local variables with the same name in different functions, because each is recognized only by the function in which it is declered

Global Variables

If you declare a variable outside a function,all the functions on your page can access it.

The lifetime of these variables starts when they are declared and ends when the page is closed.



1 comment:

suman said...

nice article for beginners.thank you.
python tutorial
java tutorial

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams