Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.

Slides:



Advertisements
Similar presentations
1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
Advertisements

1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Page 1 of 26 Javascript/Jscript Ch 7,8,9,10 Vadim Parizher Computer Science Department California State University, Northridge Spring 2003 Slides from.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
1 JavaScript/Jscript 4 Functions. 2 Introduction Programs that solve real-world programs –More complex than programs from previous chapters Best way to.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 11 – JavaScript/Jscript: Functions Outline 11.1Introduction 11.2Program Modules in JavaScript.
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
1 Lecture 3 Part 1 Functions with math and randomness.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Pemrograman Teknologi Internet W06: Functions and Events.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions Xiang Lian The University of Texas – Pan American Edinburg, TX
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved. 2 Revised by Dr. T. Tran for CSI3140.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
JavaScript, Fourth Edition
Chapter 10: JavaScript Functions CIS 275—Web Application Development for Business I.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Review for Nested loops & Math class methods & User defined methods.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
REEM ALMOTIRI Information Technology Department Majmaah University.
Functions Course conducted by: Md.Raihan ul Masood
Functions.
Methods Chapter 6.
JavaScript: Functions
JavaScript Functions.
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 10 - JavaScript: Functions
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
Chapter 6 - Functions Outline 5.1 Introduction
JavaScript: Functions
Chapter 10 - JavaScript: Functions
Presentation transcript:

Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I

Objectives constructing programs modularly from small pieces called functions. creating new functions. mechanisms used to pass information between functions. simulation techniques that use random-number generation. Scope – visibility of identifiers is limited to specific regions of programs.

Introduction Software design – Break software up into modules Easier to maintain and debug Promotes code reuse Eliminates unnecessary code repetition – Divide and conquer

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

Program Modules in JavaScript Functions – Started by function call – Receive necessary information via arguments (parameters) – Relationship Calling function vs. Called function Return value when finished Can have many tiers

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 + s2 );

Programmer-Defined Functions Defining functions – All variables declared in function are called local Do not exist outside current function – Parameters Also local variables, assume values of arguments passed into the function – Promotes reusability Keep short: if more than half page, it can probably be broken into two or more functions Name clearly

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 of parameters as function call May be empty – Declarations and statements Function body (“block” of code)

Function Definitions Returning control – return statement – Can return either nothing, or a value return expression ;

Function Definitions: example Writing a function to square a list number – See example In class exercise: – Modify this function to return a list of products of two numbers up to specified maximums

Function Definitions: Example 2 Finding the maximum of 3 numbers – Prompt for 3 inputs – Convert to numbers – Pass to maximum – Math.max In class exercise: – Find the maximum of 4 values – Find the maximum of any number of values. Use sentinel value for the end of data entry

Random-Number Generation Random-number generation introduces element of chance – Math.random var randomValue = Math.random(); – Floating point value between 0 and 1 – Adjust range by scaling and shifting – Math.floor Always round down Math.floor( 1 + Math.random() * 6 )

Example: Game of Chance Craps – Click Roll Dice – Text fields show rolls, sum and point – Status bar displays results

Example: Game of Chance Uses XHTML forms – Gather multiple inputs at once – Empty action attribute – name attribute allows scripts to interact with form Event handling and event-driven programming – Assign a function to an event – Onclick

Example: Game of Chance Constants – Variable that cannot be modified – Part of many languages, not supported in JavaScript Name “constant” variables with all capital letters – Make values easier to remember/change

Example: Game of Chance Changing properties – Access with dot (. ) notation – value property of text fields – status property of window

Outline

If the value of firstRoll is true, then function rollDice is called. If function rollDice returns a value of 7 or 11, the player wins and the break statement causes program control proceeds to the first line after the switch structure. If function rollDice returns a 2, 3 or 12, the player loses and the break statement causes control to proceed to first line after the switch structure.

Outline If the value returned by function rollDice equals the value of variable myPoint, the player wins because the point has been reached. If the values returned by function rollDice equals 7, the player loses. window method status displays a message in the status bar of the browser. If the value of firstRoll is false, function rollDice is called to see if the point has been reached.

Outline Craps.html (4 of 5) Function rollDice is called to simulate the rolling of two dice on the craps table. Methods random and floor are used to generate the values for the two dice. Referencing the names of form elements in the XHTML document, the values of the dice are placed in their respective form fields.

Outline Craps.html (5 of 5)

Example: Game of Chance Fig Craps game simulation. A text XHTML GUI componentA button XHTML GUI component Browser’s status bar

Example: Game of Chance Fig Craps game simulation.

Example: Game of Chance Fig Craps game simulation.

Scope Rules Scope – Portion of program where identifier can be referenced – Inside function is local or function scope Identifiers exist only between opening and closing braces Local variables hide global variables

Scope Rules Scope demonstration – Global variable x initialized to 1 – start has local variable x initialized to 5 – functionA has local variable x initialized to 25 – functionB has no local variable x – Observe output of each function

Outline To begin the program, variable x is initialized to 1.Function start changes the value of x to 5.

Outline Scoping.html (2 of 3) Function functionA changes the value of x to 25. The value of x is incremented.

Outline Scoping.html (3 of 3) Function functionB multiplies the value of x by 10.

Scope Rules Fig Scoping example.

JavaScript Global Functions Global object – Always available – Provides 7 methods – Do not need to explicitly reference Global before method call – Also holds all global variables, user defined functions