www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons JavaScript Functions Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
Lesson 45
Lesson 46
Lesson 47
Lesson 48
Lesson 49
JavaScript functions are named groups of code that can be used and reused again and again.

This allows us to use the code developed by other programmers and other programmers to use ours.

Defining Functions in JavaScript
Functions in JavaScript are defined using the keyword "function" followed by its name and a pair of parenthesis (). The syntax of a simple function follows:

function fun()
// JavaScript code goes here

Following is a simple JavaScript function that print the sting "Hello World!":

function fun()
document.write("Hello World!");

Suppose that we want to write a function that, instead of printing a predefined string, prints what we ask it to. For this purpose JavaScript allows us to define functions that accept parameters. The following is the syntax of a simple JavaScript function that accepts parameters:

function fun(var1, var2,...)
// JavaScript code goes here

A JavaScript function can be defined to receive any number of variables.

The following JavaScript fuction takes 2 variables as its input and prints their sum:

function printSum(var1, var2)
document.write(var1 + var2);

Calling Functions in JavaScript
Function calling in JavaScript is fairly simple. We call a function in JavaScript by writing its name followed by an empty pair of parenthesis, if the function does not accept any parameters. The following line of code calls the function fun, defined in the precedding text:


For the functions that accept parameters we pass the parameters in the pair of parenthesis. See the following line of code that calls the function printSum defined somewhere in this lesson:

printSum(12, 14);

Functions that return values
Functions in JavaScript can also return values. Functions that need to return some value must use the return statement followed by the value that is to be returned. The following function takes two values as its input and returns their sum:

function sumValues(var1, var2)
var result = var1 + var2;
return result;

var value1 = 10;
var value2 = 20;
var sum = sumValues(value1, value2);
// sum now has the value 30

Remember that the return statement immediately terminates the function and any code after the return statement would not execute.

Next >>> Lesson No. 16: JavaScript Arrays

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.