please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 5
Statements
Next
 

5.11 function

Earlier, we saw that the () operator is used to invoke a function. Before a function can be invoked, however, it must be defined (except for those that are predefined by JavaScript); the function statement is used to define a new function. It has the following syntax:

function funcname([arg1 [,arg2 [..., argn]]]) {
    statements
}

funcname is the name of the function that is being defined. This must be a literal name, not a string or an expression. arg1, arg2, and so on to argn are a comma-separated list of any number (including zero) of argument names for the function. These are also literal names, not strings or expressions. These names can be used as variables within the body of the function; when the function is executed, they will be assigned the values specified in the function call expression.

The function statement differs from statements like the while and for loops. In those loops, the body of the loop is a single statement, which can be a single primitive statement or a block of statements enclosed in curly braces. For the function statement, however, curly braces are a required part of the syntax, and any number of JavaScript statements may be contained within. Even if the body of a function consists of only a single statement, the curly braces must still be used. Here are some example function definitions:

function welcome() { alert("Welcome to my home page!"); }
function print(msg) { 
    document.write(msg, "<br>");
}
function hypotenuse(x, y) { 
    return Math.sqrt(x*x + y*y);     // return is documented below
}
function factorial(n) {              // a recursive function
    if (n <=index.html 1) return 1;  
    else return n * factorial(n - 1);
}

The most important way that the function statement differs from other statements is that the statements that form the body of the function are not executed. Instead, they are stored as the definition of a new function named funcname, and may be executed at any later time with the () function call operator.

We'll learn more about functions in Chapter 6, Functions.


Previous Home Next
var Book Index return

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/ch05_11.htm, 6KB, , updated: 2019/10/14 15:00, local time: 2024/12/22 17:14,
TOP NEW HELP FIND: 
18.188.62.10:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.org/techref/language/java/script/definitive/ch05_11.htm"> [Chapter 5] 5.11 function</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .