invoke both functions in the following manner:
write(larger(5, 13)); //Writes 13
The following figur e shows how both functions are executed .
larger(5, 13);
Main Program
write(
13
5
13
13
document.write(x);
Functions
);
x
y
x
return x > y ? x : y;
The function larger() is called first and it returns 13, which is the value of the
expression x > y ? x : y. This value is immediately used as an argument of the
function write(), which is called next and w rites the value to the browser window.
Maria: I don’t see why w rite(), which only calls documen t.write(), is usefu l.
Professor: Later you may decide to c hange how you write things out, in which case
it is very convenient that you already h ave a function defined for writing. That way,
you don’t h ave to change the cod e all over your program, but you only have to rewrite
a single function definition. Sometimes a function like that is called a p laceholder
because you define it primarily to replace it later with dierent code.
Mike: What happens if you provide a dierent number of arguments when you call a
function? We learned, for example, that a number of arguments passed to a Date()
constructo r decides about what a constructor actually does. Can we also implement
behavior like that in our fu nctions?
Professor: JavaScript won’t mind if you invoke a function with either fewer or more
arguments than de clared parameters. It’s the programmer’s responsibility to handle
those situations.
If you provide fewer arguments than declared parameters, then the missing arguments
are assumed to have the undefined value. You can use that fact to write a fu nction
that accepts op tional para meters. For example, we can write a fun ction that returns a
Euclidean norm of either a real numbe r or a two-dimensional vector, depending upon
the number of passed arguments. In n ot-so-mathematical language that m eans the
function returns either the absolute value of a real nu mber or the length of a vector. The
10.2. Writing Function Definitions 193
latter is calculated as the square root of the sum of squares of the vector components
according to the Pythagorean theorem. This is the function:
var norm = function(x, y) {
if (y === undefined) {
//Provides a default value if the second argument is omitted.
y = 0;
}
return Math.sqrt(x * x + y * y);
};
The second argument of the above function is optional. Wheth er you pass it or not,
the function knows h ow to handle either situation. This is how you can invoke the
function:
norm(-3) //Returns 3: the absolute value of a real number
norm(3, 4) //Returns 5: the length of a two-dimensional vector
Note that you must always put o ptional parameters at the end of the p arameter list.
It is also possible to provide more arguments than there are declared parameters. In
that c ase you ca n use the arguments object, which is autom atically created an d can
be accessed within the fu nction body. You can think of the ar guments object as a n
array whose elements are equal to the arguments passed to the function . You can use
a n umeric index in square brackets to access the passed arguments, and there is of
course also the length property. Technically speaking, arguments is not an array,
though.
We can rewrite our nor m() function to accept any number of arguments and thus
be able to calculate the length o f a vector of arbitrary dimension. We are going to
omit parameters from the function definition altog ether because we ca n access all the
arguments exclusively through the arguments object. The first argum ent is accessible
via arguments[0], the secon d via arguments[1], and so on. Th is is the function:
var norm = function() {
var i, sum = 0;
//Calculates the sum of squares of the passed arguments:
for (i = 0; i < arguments.length; i++) {
sum += arguments[i] * arguments[i];
}
//Calculates and returns the square root of the sum:
return Math.sqrt(sum);
};
You can use this function to compute a space diagonal of a cube with a side length of
two, for example:
194 Meeting 10. Understa nding Functions
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.220.21