JavaScript -Functions - Chapter 10 - JavaScript: Functions...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 10 - JavaScript: Functions Outline 10.1   Introduction 10.2   Program Modules in JavaScript 10.3   Programmer-Defined Functions 10.4   Function Definitions 10.5   Random-Number Generation 10.6   Example: Game of Chance 10.7 Another Example: Random Image Generator 10.8   Scope Rules 10.9   JavaScript Global Functions 10.10   Recursion 10.11   Recursion vs. Iteration 10.12   Web Resources
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Objectives In this tutorial, you will learn: To understand how to construct programs modularly from small pieces called functions. To be able to create new functions. To understand the mechanisms used to pass information between functions. To introduce simulation techniques that use random-number generation. To understand how the visibility of identifiers is limited to specific regions of programs.
Background image of page 2
10.1 Introduction Software design Break software up into modules Easier to maintain and debug Divide and conquer
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10.2 Program Modules in JavaScript Modules in JavaScript Functions Methods Belong to an object JavaScript includes many useful pre- defined methods Combine with programmer-defined methods to make a program
Background image of page 4
10.2 Program Modules in JavaScript Functions Started by function call Receive necessary information via arguments (parameters) Boss-Worker relationship Calling function Called function Return value when finished Can have many tiers
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10.2 Program Modules in JavaScript boss worker1 worker2 worker3 worker4 worker5 Fig. 10.1 Hierarchical boss-function/worker-function relationship.
Background image of page 6
10.2 Program Modules in JavaScript Function calls Name Left parenthesis Arguments separated by commas Constants, variables or expressions Right parenthesis Examples: total +=  parseFloat( inputValue ); total += parseFloat( s1 + 
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10.3 Programmer-Defined Functions Defining functions All variables declared in function are called local Do not exist outside current function Parameters Also local variables Promotes reusability Keep short Name clearly
Background image of page 8
10.4 Function Definitions Format of a function definition function   function-name parameter-list   ) {     declarations and statements } Function name any valid identifier Parameter list names of variables that will receive arguments Must have same number as function call May be empty Declarations and statements Function body (“block” of code)
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10.4 Function Definitions Returning control return statement Can return either nothing, or a value return   expression ; No return statement same as return; Not returning a value when expected is an error
Background image of page 10
10.4 Function Definitions Writing a function to square two numbers for loop from 1 to 10 Pass each number as argument to square return value of argument multiplied by itself Display result
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1 <?xml version =  "1.0"
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/06/2012 for the course FACULTY OF WXGE6320 taught by Professor Noraini during the Winter '09 term at University of Malaya.

Page1 / 48

JavaScript -Functions - Chapter 10 - JavaScript: Functions...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online