## 7.1 Defining and Invoking Functions
As
we saw in Chapter 6, the most common way to define
a function is with the The name of the function An optional comma-separated list of parameter names in parentheses The JavaScript statements that comprise the body of the function, contained within curly braces
Example 7-1 shows the definitions of several
functions. Although these functions are short and simple, they all
contain each of the elements I just listed. Note that functions may
be defined to expect varying numbers of arguments and that they may
or may not contain a
## Example 7-1. Defining JavaScript functions// A shortcut 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 = 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; return x * factorial(x-1); }
Once
a function has been defined, it may be invoked with the print("Hello, " + name); print("Welcome to my home page!"); total_dist = distance(0,0,2,1) + distance(2,1,3,5); print("The probability of that is: " + factorial(39)/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 of the function. These values are assigned to the parameters named when the function was defined, and the function operates on its parameters by referring to them by name. Note that these parameter variables are defined only 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 parameters, and JavaScript does not
check whether 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
Note that the ## 7.1.1 Nested FunctionsECMAScript v1 and implementations prior to JavaScript 1.2 allow functions to be defined only in top-level global code. JavaScript 1.2 and ECMAScript v3, however, allow function definitions to be nested within other functions. For example: function hypotenuse(a, b) { function square(x) { return x*x; } return Math.sqrt(square(a) + square(b)); } Note that ECMAScript v3 does not allow function definitions to appear
anywhere; they are still restricted to top-level global code and
top-level function code. This means that function definitions may not
appear within loops or conditionals, for example.
## 7.1.2 The Function( ) ConstructorThe var f = new Function("x", "y", "return x*y;"); This line of code creates a new function that is more or less equivalent to a function defined with the familiar syntax: function f(x, y) { return x*y; } The
Notice
that the You might well wonder what the point of the Another reason to use the ## 7.1.3 Function Literals
ECMAScript v3 defines and JavaScript
1.2 implements function literals, which are a third way to create
functions. As discussed in Chapter 3, a function
literal is an expression that defines an unnamed
function. The syntax for a function literal is much like that of the
function f(x) { return x*x; } // function statement var f = new Function("x", "return x*x;"); // Function( ) constructor var f = function(x) { return x*x; }; // function literal Although function literals create unnamed functions, the syntax allows a function name to be optionally specified, which is useful when writing recursive functions that call themselves. For example: var f = function fact(x) { if (x <= 1) return 1; else return x*fact(x-1); }; This line of code defines an unnamed function and stores a reference
to it in the variable Keep in mind that the Function literals are useful in much the same way as functions
created with the a[0] = function(x) { return x*x; }; // Define a function and store it a.sort(function(a,b){return a-b;}); // Define a function; pass it to another var tensquared = (function(x) {return x*x;})(10); // Define and invoke Like the |