Chapter 20: Programming Functions

Slides:



Advertisements
Similar presentations
Chapter 19 Programming Functions. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives Apply JavaScript rules.
Advertisements

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
Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
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.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
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.
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
1 JavaScript in Context. Server-Side Programming.
Introduction to JavaScript Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan. 1.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
THINKING BIG Abstraction and Functions chapter 6 Modified by Dr. Paul Mullins for CPSC 130, F05.
JavaScript III Functions and Abstraction. 2 JavaScript so far statements assignment function calls data types numeric string boolean expressions variables.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
Introduction to Programming JScript Six Scripting functions Discuss functions Password Example.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
JavaScript, Fourth Edition
Chapter 10: JavaScript Functions CIS 275—Web Application Development for Business I.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
HTML Overview Part 5 – JavaScript 1. Scripts 2  Scripts are used to add dynamic content to a web page.  Scripts consist of a list of commands that execute.
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.
Functions Function is a standalone block of statements that performs some tasks and returns a value. Functions must be declared before they can be used.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
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.
Midterm 2 Review. What does it mean to “declare a variable” in JavaScript? Write code to declare a variable with a name of your own choosing, in Javascript.
Tarik Booker CS 120 California State University, Los Angeles.
REEM ALMOTIRI Information Technology Department Majmaah University.
Learning Objectives Apply JavaScript rules for functions, declarations, return values, function calls, scope of reference, and local/global variable reference.
Event-Driven Programming
Chapter 9: Value-Returning Functions
Chapter 6 JavaScript: Introduction to Scripting
“Under the hood”: Angry Birds Maze
Abstraction and Functions
Learning to Program D is for Digital.
© 2015, Mike Murach & Associates, Inc.
Fluency with Information Technology
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
JavaScript: Functions
JavaScript Functions.
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
Chapter 10 - JavaScript: Functions
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
Use of Mathematics using Technology (Maltlab)
INFO/CSE 100, Spring 2005 Fluency in Information Technology
PHP.
JavaScript What is JavaScript? What can JavaScript do?
INFO/CSE 100, Spring 2006 Fluency in Information Technology
JavaScript MCS/BCS.
JavaScript What is JavaScript? What can JavaScript do?
Training & Development
Chapter 7 Event-Driven Pages
Javascript Chapter 19 and 20 5/3/2019.
Web Programming and Design
Top-Down Design with Functions
Presentation transcript:

Chapter 20: Programming Functions TECH1010-02 Prof. Jeff Cheng

Anatomy of a Function Functions are packages for algorithms 3 parts Name Parameters Definition These parts are the function declaration Defined within the <script> tags of an HTML document

Pick a Name Name is the identifier for the function Commonly used to describe what the function does Function declaration form: function <name> ( <parameter list> ) { <statement list> }

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 function convertC2F ( tempInC ) { <statement list> }

Definition Definition is the algorithm written in a programming language To say what the answer/result is, JavaScript uses the statement: return <expression> function convertC2F ( tempInC ) { return 9.0 / 5.0 * tempInC + 32; } "Calling" a function is to run or execute it Write the function’s name, put the input values (arguments) in the parentheses convertC2F( 38 )

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

Forms and Functions Construct a web page in which to run a function Recall <form> and <input /> tags and event handlers in HTML Event handlers usually implemented as functions Using an input window, the value in that window can be used as an argument to a function

Calling to Customize a Page Three ways to get the result of a function call to print on the monitor 1) Before the page is created For example, with the alert() call (Fig. 20.1) 2) Interactively after the page is displayed For example, the Conversion application (Fig. 20.2) 3) While the page is being loaded For example, document.write() built-in function Calling functions while the browser is creating the page allows us to customize pages on-the-fly

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()

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 <script> </script> tags to create the rows of the table Put Celsius values in first column cells, second column cells can call conversion function

Predefined Functions Math.random() Math.round() Math.floor() Math.ceiling() Date.toString()

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) Parameters are considered local variables Variable names declared outside any function can be used throughout the program (global to the function)

An Annotated Example

Scoping Local variables 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

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; }

Global/Local Scope Interaction (cont'd) y is globally declared and can be referenced anywhere y is also declared as a local variable 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

Recap: Two Reasons to Write Functions Packaging algorithms into functions Reuse Building blocks of future programming Make them as general as possible Complexity management Help us keep our sanity while we're solving problems