Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 20-2 Anatomy of a Function Functions are packages for.

Slides:



Advertisements
Similar presentations
17 HTML, Scripting, and Interactivity Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and.
Advertisements

Modular Programming With Functions
Chapter 19 Programming Functions. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives Apply JavaScript rules.
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
The Web Warrior Guide to Web Design Technologies
Word Lesson 7 Working with Documents
The Information School of the University of Washington Oct 23 fit functions 1 Functions / If Else INFO/CSE 100, Fall 2006 Fluency in Information.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
Chapter 19 Programming Functions. Learning Objectives Apply JavaScript rules for functions, declarations, return values, function calls, scope of reference,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Programming Functions Thinking Big lawrence snyder c h a p t e r 20.
CIS101 Introduction to Computing Week 12 Spring 2004.
Introduction to JavaScript for Python Programmers
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
Chapter 13 Fill-in-the-blank Computing: The Basics of Spreadsheets.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
1 DESKTOP TIPS Presented by: Doug Bonebrake Senior Project Manager Global Project Office FrontRange Solutions Inc.
Learning Objectives Apply JavaScript rules for functions, declarations, return values, function calls, scope of reference, and local/global variable reference.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Test 2 Part a You have 20 minutes Don’t forget to put your name on the test Closed book No computers Do your own work.
The Bean Counter: A JavaScript Program
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and check boxes using HTML Add a pull-down.
Lecture # 6 Forms, Widgets and Event Handling. Today Questions: From notes/reading/life? Share Personal Web Page (if not too personal) 1.Introduce: How.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 27 - Phone Book Application Introducing Multimedia.
Using Client-Side Scripts to Enhance Web Applications 1.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
THINKING BIG Abstraction and Functions chapter 6 Modified by Dr. Paul Mullins for CPSC 130, F05.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
JavaScript, Fourth Edition
JavaScript Challenges Answers for challenges
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
Chapter 14 Fill-in-the-blank Computing: The Basics of Spreadsheets.
1/25/2016B.Ramamurthy1 Exam3 Review CSE111 B.Ramamurthy.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Controlling Program Flow with Decision Structures.
 Labs next week:  Both labs will be posted on Monday  Both will be due the following Monday  Your decision on which to do first ▪ Web App Design Lab.
CSD 340 (Blum)1 Introducing Text Input elements and Ifs.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
JavaScript 101 Lesson 6: Introduction to Functions.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Learning Objectives Apply JavaScript rules for functions, declarations, return values, function calls, scope of reference, and local/global variable reference.
Unit 2 Technology Systems
Event-Driven Programming
Introduction to Programming
Fluency with Information Technology
Section 17.1 Section 17.2 Add an audio file using HTML
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Learning Objectives Apply JavaScript rules for functions, declarations, return values, function calls, scope of reference, and local/global variable reference.
JavaScript Functions B. Ramamurthy 11/22/2018.
Event Driven Programming & User Defined Functions
© Akhilesh Bajaj, All rights reserved.
Chapter 20: Programming Functions
Chapter 7 Event-Driven Pages
Javascript Chapter 19 and 20 5/3/2019.
Web Programming and Design
Presentation transcript:

Chapter 20 Thinking Big: Functions

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for algorithms 3 parts –Name –Parameters –Definition These parts are the function declaration

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Pick a Name Name is the identifier for the function –Commonly used to describe what the function does Function declaration form: function ( ){ }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Parameters Parameters are the values the function will compute on, the input values They are given names Listed parameters are separated by commas Parameter names follow usual rules for identifiers

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Definition Definition is the algorithm written in a programming language To say what the result is, JavaScript statement is: Return To run or execute the function, call it –Write the function’s name, put the input values in parentheses

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Declaration versus Call A function’s declaration is different from its call (use) Functions are declared once Functions can be called as many times as their answers are needed

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Forms and Functions To construct a web page in which to run a function Recall tag and event handlers in HTML Using an input window, the value in that window can be used as an argument to a function

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Calling to Customize a Page Three ways to get the result of a function call to print on the monitor –Before the page is created –Interactively after the page is displayed –While the page is being loaded Calling functions while the browser is creating the page allows us to customize pages on the fly

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Calling to Customize a Page How a browser builds a page: –Reads through HTML file, figuring out all tags and preparing to build page –Removes JavaScript tags and all text between them, and does whatever the JavaScript tells it to do It could tell the browser to put some text back in the file, as in document.write()

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Calling to Customize a Page Suppose we want a table of temperature conversions for a web page with a column for Celsius and a column for Fahrenheit Put document.write within the tags to create the rows of the table Put Celsius values in first column cells, second column cells can call conversion function

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Writing Functions, Using Functions Flipping Electronic Coins –A coin flip is an unpredictable event whose two outcomes are “equally probable” –Computers can generate pseudo-random numbers An algorithm that produces a sequence of numbers that passes the statistical tests for randomness We can just call them random numbers

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Flipping Electronic Coins Math.random() is JavaScript’s built in function for generating random numbers –Each time it is called, it generates a random number between 0 and 1, but never exactly equal to 0 or 1 A function to flip electronic coins: Function coinFlip(){ return Math.round(Math.random()); }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Flipping Electronic Coins (cont’d) coinFlip() returns with equal probability a 0 or a 1 Next improvement is to return the text Heads or Tails rather than numbers Function flipText() { if (coinFlip()==0) return ‘Tails’; else return ‘Heads’; }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Flipping Electronic Coins (cont’d) Even more useful to give outcome in response to pressing a button on a web page

Copyright © 2006 Pearson Addison-Wesley. All rights reserved The Body Mass Index Computation BMI is a standard measure of weight in proportion to height Formula (in metric units): –Index = weight/height 2 Two parameters for this function, weight and height function bmiM (weightKg, heightCM){ var heightM = heightCm/100; return weightKg/ (heightM * heightM); }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved The Body Mass Index Computation (cont'd) Formula (in English units): Index = 4.89 weight / height 2 Function: function bmiE ( weightLbs, heightIn ){ var heightFt = heightIn/12; return 4.89 * weightLbs / (heightFt * heightFT); }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved The Body Mass Index Computation (cont'd) Function that could calculate BMI in type of units specified by user would need 3 inputs (kind of unit, weight, height) function BMI ( units, weight, height) { if (units == ‘E’) return bmiE (weight, height); else return bmiM (weight, height): }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved The Body Mass Index Computation (cont'd) To put this function in a web page, we add radio buttons to select type of units Two new features of radio buttons: –All related buttons share same name (clicking one on turns the other off) –Can be preset using checked=‘true’ Add event handlers for the radio buttons

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Scoping: When to Use Names Scope of a name defines how “far” from its declarations it can be used General rule for scoping: –Variable names declared in a function can be used only within that function (they are local to the function) –Variable names declared outside any function can be used throughout the program (global to the function)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved An Annotated Example

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Scoping Locals come into existence when a function begins, and when it ends, they vanish Global variables are around all the time If information must be saved from one function call to the next, it must be in a global variable

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Global/Local Scope Interaction Where a global variable and a local variable have the same name: var y=0; … function tricky (x) { var y; y=x; … }

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Global/Local Scope Interaction (cont'd) y is globally declared and can be referenced anywhere y is also declared as a local in the tricky() function They are two different variables Which y is assigned the parameter x? –The local y, because it is declared in the function’s scope, making it the closest declaration and hiding the global y

Copyright © 2006 Pearson Addison-Wesley. All rights reserved The Memory Bank Web Page Create a web page for remembering useful computations and storing them in an interactive form Practice programming with functions

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Plan the Memory Bank Web Page Each table row presents a computation Each text box except the last is an input to the computation The last text box is for the output Start with the row from the BMI computation page

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Random Additions Add the row from the coin-flipping page Program event handler to keep track of the number of heads and tails flipped Use global variables so they keep their values across function calls

Copyright © 2006 Pearson Addison-Wesley. All rights reserved The I’m Thinking of a Number Row Guessing game – choose a number from 1 to n Use randNum() function, but shift the range by 1 –randNum(n)+1; This table row is similar to coin-flipping row, but has a text box to set the upper end of the range –Declare global variable (topEnd) to say what the limit of the range is –When the user clicks button, the randNum() function is called with topEnd as the argument, and the result is incremented to shift its range. The result is displayed

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Improving the Memory Bank Web Page Needs to be fancier and include more features Program the memory bank to splash new pages onto the screen Unlike a link, this allows both pages to display at the same time

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved A Counting Page To keep track of counts of things Counter Assistant application: –Count button increments Total field –Meaning field can be filled in with any text to remind us what the counter is –C clears all the fields

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Add Final Touches to Memory Bank Add a date –JavaScript Date().toString() References the date object, which contains the current date and time in numeric form, and converts to a printable form Add web links –Add any useful links (online dictionary, etc) in their own column or in a row at the bottom of the table

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Assess the Web Page Design Table data spans two columns using colspan=2 Links are grouped by topic Red bullet is used to separate entries Link area has a neat structure; adding new links is easy

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved