Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
E pluribus unum.(One composed of many.)
O! call back yesterday, bid time return.
Call me Ishmael.
When you call me that, smile.
In this chapter you will learn:
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.11 Recursion vs. Iteration
9.13 Web Resources
Worker1 is boss of worker4 and worker5
worker1, worker2 and worker3 report back to boss
Worker4 and worker5 report back to worker1Fig. 9.1 | Hierarchical boss-function/worker-function relationship.
If a function’s task cannot be expressed concisely, perhaps the function is performing too many different tasks. It is usually best to break such a function into several smaller functions.
function function-name( parameter-list )
declarations and statements
Calls function square with x as an argument, which will return the value to be inserted here
Begin function square
Names the parameter y
End function square
Returns the value of y * y (the argument squared) to the caller
Place a blank line between function definitions to separate the functions and enhance program readability.
Statements that are enclosed in the body of a function definition are not executed by the Java-Script interpreter unless the function is invoked explicitly.
Forgetting to return a value from a function that is supposed to return a value is a logic error.
Redefining a function parameter as a local variable in the function is a logic error.
Although it is not incorrect to do so, do not use the same name for an argument passed to a function and the corresponding parameter in the function definition. Using different names avoids ambiguity.
To promote software reusability, every function should be limited to performing a single, well-defined task, and the name of the function should express that task effectively. Such functions make programs easier to write, debug, maintain and modify.
A small function that performs one task is easier to test and debug than a larger function that performs many tasks.
Creates integer values from user input
Variable maxValue stores the return value of the call to maximum
Calls function maximum with arguments value1, value2 and value3
Begin function maximum with local variables x, y and z
Calls the Math object’s method max to compare the first variable with the maximum of the other two
End function maximum
Shifts the range of return values up by 1
Scales the range of return values by a factor of 6
Takes the floor of the number from 1.0 up to, but not including, 7.0
Variable value has a value from 1 to 6
Stores a random integer in the range 1 – 6 to variable face
Uses a switch statement to process the results
Outputs the number of occurrences of each face being rolled
Running the script another time calls Math.random again and (usually) results in different frequencies
statusDiv holds the object with “status” as its id
switch statement performs actions for specific dice values
Sets the contents of the statusDiv object to “Roll Again”
Sets the contents of the statusDiv object to “Player wins. Click Roll Dice to play again.”
Sets the contents of the statusDiv object to “Player loses. Click Roll Dice to play again.”
Sets the id fields of these container objects so that they can be retrieved by getElementById in the script
Sets the values of the die1field, die2field and sumfield objects to die1, die2 and workSum, respectively
When the Roll Dice button is clicked, function play is called
Use only uppercase letters (with underscores between words) in the names of variables that should be used as constants. This format makes such variables stand out in a program.
Use meaningfully named variables rather than literal values (such as 2) to make programs more readable.
Variables that are declared inside the body of a function are known only in that function. If the same variable names are used elsewhere in the program, they will be entirely separate variables in memory.
Initializing variables when they are declared in functions helps avoid incorrect results and interpreter messages warning of uninitialized data.
Creates an src attribute by concatenating a random integer from 1 to 7 with “.gif\” to reference one of the images 1.gif, 2.gif, 3.gif, 4.gif, 5.gif, 6.gif or 7.gif
Avoid local-variable names that hide global-variable names. This can be accomplished by simply avoiding the use of duplicate identifiers in a script.
Global variable declaration
Local variable in function start
Local variable in function functionA, initialized each time functionA is calledFig. 9.8 | Scoping example (Part 2 of 3).
Calls function start when the body of the document has loaded into the browser window
While the number to be processed is greater than 1, the function calls itself
When the number is 1 or less (the base case), 1 is returned, and each function call can now return an answer until each call has been resolvedFig. 9.10 | Recursive evaluation of 5!.
Calls function factorial with argument i
While the base case is not reached, return the number * ( number – 1 )!, which is number *factorial ( number – 1 )
factorial calls itself with a new argument and waits until this new value is returned before returning a value itself
Forgetting to return a value from a recursive function when one is needed results in a logic error.
Omitting the base case and writing the recursion step incorrectly so that it does not converge on the base case are both errors that cause infinite recursion, eventually exhausting memory. This situation is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution.
Internet Explorer displays an error message when a script seems to be going into infinite recursion. Firefox simply terminates the script after detecting the problem. This allows the user of the web page to recover from a script that contains an infinite loop or infinite recursion.
Any problem that can be solved recursively can also be solved iteratively (nonrecursively). A recursive approach is normally chosen in preference to an iterative approach when the recursive approach more naturally mirrors the problem and results in a program that is easier to understand and debug. Another reason to choose a recursive solution is that an iterative solution may not be apparent.
Avoid using recursion in performance-oriented situations. Recursive calls take time and consume additional memory.
Accidentally having a nonrecursive function call itself, either directly, or indirectly through another function, can cause infinite recursion.