BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
JavaScript
JavaScript Form
JavaScript Error (try/catch/finally)
JavaScript Date Object Functions
JavaScript Date Object
JavaScript Loops
JavaScript Operators
JavaScript Conditional Statements
JavaScript Boolean Object
JavaScript Assertiveness
JavaScript Array Object Functions
JavaScript Arrays
JavaScript Variables
How to use JavaScript?
JavaScript Guidelines
Stock sorter example for IE5
Confused about how to use a data island in IE5??
Software Release tracking application
Employee Phonebook dataisland
Multitab DHTML browser
Trim function in Javascript
 
LAMP >>

By :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.

JavaScript Simple Function
JavaScript Functions with Parameters
JavaScript Functions with Return Values  

JavaScript Simple Function

<html>

<head>

<script type="text/javascript">

            // Create a function

            function X()

            {

                        document.write("This is a simple function.<br/>")

            }

            // Call the function

            X()

</script>

</head>

<body></body>

</html>

Output

JavaScript Functions with Parameters

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/>")

            var PI = 3.14

            return number*PI

}

// Call function, which will return a value.

var x = ReturnPIValue(5)

document.write(x)

Output


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

An immediate job opportunity as a B
Software Developers Needed in Charl
Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C

View all Jobs (Add yours)
View all CV (Add yours)



answering service
unlimited conferencing
swimming pool contractor
large conference calls
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP