Skip to main content

User Defined Functions

A function is a group of related JavaScript statements that exist separately from the rest of the script and perform one or more designated tasks. Functions are also useful if you need to control exactly when a particular task occurs. The basic structure of a function is a follows:

 
function Function_Name([Arguments]) {
    JavaScript statements
}
 

The function keyword identifies the procedure as a function. Function Name this is a unique name for the function. Example: Calling a function:

 
<html>
<head>
<title>Calling a Function</title>
<script language="JavaScript" type="text/JavaScript">
<!--

function make_background_red() { 
    document.bgColor = "red"
    document.write ("<h1>The background is now red.<\/h1>")
} 
make_background_red()
//-->
</script>
</head>
<body>
</body>
</html>
 

Next: Passing Parameters to Functions