W3Schools

 

home HOME

JS Basic
JS HOME
JS Introduction
JS How To
JS Where To
JS Variables
JS Operators
JS Functions
JS Conditional
JS Looping
JS Guidelines

JS References
JS String Object
JS Array Object
JS Date Object
JS Math Object
JS Window Object
JS Frame Object
JS Form Object
JS Browser Object

Examples/Quiz
JS Examples
JS Quiz

JavaScript Functions

previous next

A function is a reusable code-block that will be executed by an event, or when the function is called.


Examples

Function
How to call a function.

Function with arguments
How to pass a variable to a function, and use the variable value in the function.

Function with arguments 2
How to pass variables to a function, and use these variable values in the function.

Function that returns a value
How to let the function return a value.

A function with arguments, that returns a value
How to let the function find the sum of 2 arguments and return the result.


Functions

A function contains some code that will be executed by an event or a call to that function. A function is a set of statements. You can reuse functions within the same script, or in other documents. You define functions at the beginning of a file (in the head section), and call them later in the document. It is now time to take a lesson about the alert-box:

This is JavaScript's method to alert the user.

alert("This is a message")


How to Define a Function

To create a function you define its name, any values ("arguments"), and some statements:

function myfunction(argument1,argument2,etc)
{
some statements
}

A function with no arguments must include the parentheses:

function myfunction()
{
some statements
}

Arguments are variables used in the function. The variable values are values passed on by the function call.

By placing functions in the head section of the document, you make sure that all the code in the function has been loaded before the function is called.

Some functions return a value to the calling expression

function result(a,b)
{
c=a+b
return c
}


How to Call a Function

A function is not executed before it is called.

You can call a function containing arguments:

myfunction(argument1,argument2,etc)

or without arguments:

myfunction()


The return Statement

Functions that will return a result must use the "return" statement. This statement specifies the value which will be returned to where the function was called from. Say you have a function that returns the sum of two numbers:

function total(a,b)
{
result=a+b
return result
}

When you call this function you must send two arguments with it:

sum=total(2,3)

The returned value from the function (5) will be stored in the variable called sum.


previous next

Product Spotlight


Hosting Spotlight


Phone Cards

Phone Cards Online

International and domestic phone cards for US customers.

Great affiliate program.


We Help You For Free. You Can Help Us!


Jump to: Top of Page or HOME or Printer Friendly Printer friendly page


Search W3Schools:


What Others Say About Us

Does the world know about us? Check out these places:

Google  Yahoo  Alta Vista  MSN  Lycos  Excite  Ask Jeeves  HotBot


W3Schools is for training only. We do not warrant the correctness of the content. The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2004 by Refsnes Data. All Rights Reserved.


Validate How W3Schools was converted to XHTML Validate

Web charting
Web based charting
for ASP.NET


WEB HOSTING

$15 Domain Name
Registration
Save $20 / year!



UK Domain Names
Web Hosting
Merkaweb Dominios


Buy UK Domain Names
Register Domain Names


Website Hosting
Domain Hosting
Web Hosting


Cheap Domain Names UK


Domain Names UK
Quality Web Hosting
Webmail & POP3 Email


Web Hosting UK
Dedicated Servers UK
Domain Names UK


Business Web Hosting
Free Web Hosting
Cheap Web Hosting
Web Hosting Reseller


Website Design UK

HOTELS

Amsterdam Hotels
Boston Hotels
Chicago Hotels
London Hotels
Los Angeles Hotels
Miami Hotels
New York Hotels
New Orleans Hotels
Paris Hotels
Rome Hotels
Hotel Reservations


Hotel Directory
Hotel Rooms

Web Building

Website Templates
Website Builder
Flash Templates
Web Templates

Shopping

Garden decor
Online Coupons
Cooking tools
Men & women clothing
Office supply

Software

Student Price Software


Your Own Web Site?

Read W3Schools
Hosting Tutorial








WebDev Books

The Web Design Business Kit Grow Your Web Design Business!

HTML Utopia: Designing Without Tables Using CSS Learn CSS Positioning the easy way!

Build Your Own Database Driven Website Using PHP & MySQL Learn PHP & MySQL the easy way!

SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Security
Web Statistics
Web Standards

Web Quality