Ch9 Functions
Ch9 Functions
JavaScript: Functions
OBJECTIVES
In this chapter you will learn:
To construct programs modularly from
small pieces called functions.
To create new functions.
How to pass information between
functions.
Simulation techniques that use random
number generation.
How the visibility of identifiers is limited to
specific regions of programs.
9.1 Introduction
9.2 Program Modules in JavaScript
9.3 Programmer-Defined Functions
9.4 Function Definitions
9.5 Random Number Generation
9.6 Example: Game of Chance
9.7 Another Example: Random Image Generator
9.8 Scope Rules
9.9 JavaScript Global Functions
9.10 Recursion
9.11 Recursion vs. Iteration
9.12 Wrap-Up
9.13 Web Resources
9.1 Introduction
Worker1 is boss of
worker4 and worker5
isFinite Takes a numeric argument and returns true if the value of the argument is not NaN,
Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY (values that are not
numbers or numbers outside the range that JavaScript supports)—otherwise, the function returns
false.
isNaN Takes a numeric argument and returns true if the value of the argument is not a number;
otherwise, it returns false. The function is commonly used with the return value of parseInt
or parseFloat to determine whether the result is a proper numeric value.
parseFloat Takes a string argument and attempts to convert the beginning of the string into a floating-point
value. If the conversion is unsuccessful, the function returns NaN; otherwise, it returns the converted
value (e.g., parseFloat( "abc123.45" ) returns NaN, and parseFloat(
"123.45abc" ) returns the value 123.45).
parseInt Takes a string argument and attempts to convert the beginning of the string into an integer value. If
the conversion is unsuccessful, the function returns NaN; otherwise, it returns the converted value
(e.g., parseInt( "abc123" ) returns NaN, and parseInt( "123abc" ) returns the integer value 123).
This function takes an optional second argument, from 2 to 36, specifying the radix (or base) of the
number. Base 2 indicates that the first argument string is in binary format, base 8 indicates that the
first argument string is in octal format and base 16 indicates that the first argument string is in
hexadecimal format. See Appendix E, Number Systems, for more information on binary, octal and
hexadecimal numbers.
unescape Takes a string as its argument and returns a string in which all characters previously encoded with
2008 Pearson Education,
escape are decoded.
Inc. All rights reserved.
62
9.10 Recursion
• A recursive function calls itself, either directly, or
indirectly through another function.
• A recursive function knows how to solve only the simplest
case, or base case
– If the function is called with a base case, it returns a result
– If the function is called with a more complex problem, it divides the
problem into two conceptual pieces—a piece that the function knows
how to process (the base case) and a simpler or smaller version of the
original problem.
• The function invokes (calls) a fresh copy of itself to go to
work on the smaller problem; this invocation is referred
to as a recursive call, or the recursion step.