JavaScript: The Definitive Guide

Previous Chapter 6 Next

 


6. Functions

Contents:
Defining and Invoking Functions
Functions as Data Types
The Function Object
Built-in Functions
Event Handlers

Functions are an important and complex part of the JavaScript language. This chapter examines functions from several points of view. First, functions are introduced from the syntactic standpoint, explaining how functions are defined and invoked. Second, it is shown that functions are data types in JavaScript, with examples of the useful programming techniques that are possible by treating functions as data. Finally, the Function object and its properties are discussed, which support a number of advanced techniques for manipulating functions and their arguments.

Functions in JavaScript are closely integrated with JavaScript objects, and there are features of functions that are not documented in this chapter. Chapter 7, Objects, explains the specialized uses of functions as methods, constructors, and event-handlers.

6.1 Defining and Invoking Functions

As we saw in Chapter 5, Statements, functions are defined with the function keyword, followed by:

Example 6.1 shows the definition of several functions. Although these functions are short and very simple, they all contain each of the elements listed above. Note that functions may be defined to expect varying numbers of arguments, and that they may or may not contain a return statement. The return statement was introduced in Chapter 5, Statements; it causes the function to stop executing and return the value of its expression (if any) to the caller. If a function does not contain a return statement, then it simply executes each statement in the function body and returns no value to the caller.

Example 6.1: Defining JavaScript Functions

// A short-cut function, sometimes useful instead of document.write()
// This function has no return statement, so it returns no value.
function print(msg)
{
    document.write(msg, "<BR>");
}
// A function that computes and returns the distance between two points.
function distance(x1, y1, x2, y2)
{
    var dx =index.html (x2 - x1);
    var dy = (y2 - y1);
    return Math.sqrt(dx*dx + dy*dy);
}
// A recursive function (one that calls itself) that computes factorials.
// Recall that x! is the product of x and all positive integers less than it.
function factorial(x)
{
    if (x <= 1) 
        return 1;
    else 
        return x * factorial(x-1);
}

Once a function has been defined, it may be invoked with the () operator, introduced in Chapter 4, Expressions and Operators. Recall that the parentheses appear after the name of the function, and that a comma-separated list of argument values (or expressions) appear within the parentheses. The functions defined in Example 6.1 could be invoked with code like the following:

print("Hello, " + name);
print("Welcome to my home page!");
total_dist =index.html distance(0,0,2,1) + distance(2,1,3,5);
print("The probability of that is: " + factorial(13)/factorial(52));

When you invoke a function, each of the expressions you specify between the parentheses is evaluated, and the resulting value is used as an argument or parameter of the function. These values are assigned to the variables named (within parentheses) when the function was defined, and the function operates on its parameters by referring to them by name. Note that these parameter variables are only defined while the function is being executed; they do not persist once the function returns.

Since JavaScript is an untyped language, you are not expected to specify a data type for function arguments, and JavaScript does not check that you have passed the type of data that the function expects. If the data type of an argument is important, you can test it yourself with the typeof operator. JavaScript does not check that you have passed the correct number of arguments, either. If you pass more arguments than the function expects, the extra values will simply be ignored. If you pass fewer than expected, then some of the parameters will be given the undefined value--which will, in many circumstances, cause your function to behave incorrectly. Later in this chapter we'll see a technique you can use to test that the correct number of arguments have been passed to a function.

Note that because our print() function does not contain a return statement and does not return a value, it cannot be used as part of a larger expression. The distance() and factorial() functions, on the other hand, can be used as parts of larger expressions, as shown in the examples above.


Previous Home Next
Summary of JavaScript Statements Book Index Functions as Data Types


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