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.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #5 : JAVASCRIPT 2 GUESSING GAME By Wendy Sharpe 1.
Chapter 19 Programming Functions. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives Apply JavaScript rules.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
The Information School of the University of Washington Oct 23 fit functions 1 Functions / If Else INFO/CSE 100, Fall 2006 Fluency in Information.
Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Programming Functions Thinking Big lawrence snyder c h a p t e r 20.
Chapter 15: Operator Overloading
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
CS 31 Discussion, Week 4 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Learning Objectives Apply JavaScript rules for functions, declarations, return values, function calls, scope of reference, and local/global variable reference.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
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.
Announcements Assignment 9 is due Project 2 is due 7/27/04 We will be skipping chapter 22 Test 3 (chapters 18-21) will be on 7/29/04 Tip of the Day : Functions.
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.
David Stotts Computer Science Department UNC Chapel Hill.
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.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Today’s Announcements Assignment 8 is due Project 2 is due 7/27/04 We will be skipping chapter 22 Test 3 (chapters 18-21) will be on 7/29/04 Tip of the.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 3 FUNCTIONS, METHODS & OBJECTS WHAT IS A FUNCTION?
Chapter 3: User-Defined Functions I
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 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.
PHP (cont.). 2 Functions Definition – function name(arguments){... } –syntax name as for a variable, but without $ arguments as for variables –to return.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
JavaScript Modularity. Goals By the end of this lecture, you should … Understand why programmers use modularity. Understand how to create a function in.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
TK1924 Program Design & Problem Solving Session 2011/2012
Chapter 6: User-Defined Functions I
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
JavaScript: Functions
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
User-Defined 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.
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
Chapter 20: Programming Functions
INFO/CSE 100, Spring 2005 Fluency in Information Technology
Javascript Chapter 19 and 20 5/3/2019.
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Programming Basics Review
Web Programming and Design
Presentation transcript:

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

Test 2 Part b You have 60 minutes Don’t forget your name Open book, open notes The starters are file in your U:/ folder You can use your computer (recommended) Do your own work Staple all of your work together when you are finished.

Announcements Assignment 7 is due for full credit Assignments 1-4 can no longer be handed in Last day to hand in Assignment 5 for reduced credit. Tip of the Day : Browsers don’t process JavaScript the same … compare IE and Netscape if stuck

Functions & Abstraction A function is a package for an algorithm; once written, it can be use over and over. © 2004, Lawrence Snyder

The Package Functions have a specific syntax function ( ) { } names are identifiers; start w/letter is the input variables, a list separated by commas is just the program to do the work Brackets appear here by convention

A Sample Function Compute the Body Mass Index when the inputs are in metric function ( ) { } function bmiM ( weightKg, heightM ) { // Figure Body Mass Index in metric units return weightKg / (heightM * heightM); } function bmiM ( weightKg, heightM ) { // Figure Body Mass Index in metric units return weightKg / (heightM * heightM); } Identify the corresponding parts

Writing Functions Most programming is done by writing functions, so learning the form is key function bmiE ( weightLBS, heightIn ) { // Figure Body Mass Index in English units var heightFt = heightIn / 12; // Change to feet return 4.89 * weightLBS / (heightFt * heightFt); } function bmiE ( weightLBS, heightIn ) { // Figure Body Mass Index in English units var heightFt = heightIn / 12; // Change to feet return 4.89 * weightLBS / (heightFt * heightFt); }

Declarations A function is declared by writing down the “package” … the function is used when it is called function BMI (units, height, weight ) { // Compute BMI in either metric or English if (units == "English") return bmiE(weight, height); else return bmiM(weight, height); } function BMI (units, height, weight ) { // Compute BMI in either metric or English if (units == "English") return bmiE(weight, height); else return bmiM(weight, height); } Declaration Calls

Summarizing Declaration: the function “package,” says what happens when the function runs Call: the function use, asks for the computation to be run There is only one function declaration There can be many calls … functions are reusable In JS, functions tend to be grouped together but the calls go where they are needed

Gold Function Suppose we compute “weight in Au” Worth = (Weight*12)*368.4 Begin with the form... function ( ) { // Compute the dollar value // of weight at $368.40/tz } function ( ) { // Compute the dollar value // of weight at $368.40/tz }

Gold Function Suppose we compute “weight in Au” Worth = (Weight*12)*368.4 function worthau ( ) { // Compute the dollar value // of weight at $368.40/tz } function worthau ( ) { // Compute the dollar value // of weight at $368.40/tz } Pick a Name

Gold Function Suppose we compute “weight in Au” Worth = (Weight*12)*368.4 function worthau ( weight ) { // Compute the dollar value // of weight at $368.40/tz } function worthau ( weight ) { // Compute the dollar value // of weight at $368.40/tz } Pick a Name Pick the Parameter

Gold Function Suppose we compute “weight in Au” Worth = (Weight*12)*368.4 function worthau ( weight ) { // Compute the dollar value // of weight at $368.40/tz return weight * 12 * 368.4; } function worthau ( weight ) { // Compute the dollar value // of weight at $368.40/tz return weight * 12 * 368.4; } Pick a Name Pick the Parameter Define the Computation

Testing Template No one writes perfect programs the first time … smart programmers check To test, have a standard page handy My Test Page Put your JavaScript code here My Test Page Put your JavaScript code here

Declare the Function Put a function declaration in My Test Page function worthau ( weight ) { // Compute the dollar value // of weight at $368.40/troy oz return weight * 12 * 368.4; } alert(worthau(1/12)); My Test Page function worthau ( weight ) { // Compute the dollar value // of weight at $368.40/troy oz return weight * 12 * 368.4; } alert(worthau(1/12)); Testing Template

Try The Function Unquestionably, the best practice is to test everything

Function Features Reviewing properties of functions Selecting names … don’t use alert() Parameter variables … don’t need to be declared, but work like local variables function bmiE ( weightLBS, heightIn ) { // Figure BMI in English var heightFt = heightIn / 12; // Change to feet return 4.89 * weightLBS / (heightFt * heightFt); } function bmiE ( weightLBS, heightIn ) { // Figure BMI in English var heightFt = heightIn / 12; // Change to feet return 4.89 * weightLBS / (heightFt * heightFt); } function bmiE ( weightLBS, height ) { // Figure BMI in English (height in in) height = height / 12; // Change to feet return 4.89 * weightLBS / (height * height); } function bmiE ( weightLBS, height ) { // Figure BMI in English (height in in) height = height / 12; // Change to feet return 4.89 * weightLBS / (height * height); }

Function Features (cont.) Scope of Reference … refers to where in the program a variable is “known,” i.e. where its value can be referenced and/or assigned function bmiE ( weight, heightIn ) { // Figure BMI in English var heightFt = heightIn / 12; // Change to feet return 4.89 * weight / (heightFt * heightFt); } function bmiE ( weight, heightIn ) { // Figure BMI in English var heightFt = heightIn / 12; // Change to feet return 4.89 * weight / (heightFt * heightFt); } function BMI (units, height, weight ) { // Compute BMI if (units == "English") return bmiE(weight, height); else return bmiM(weight, height); } function BMI (units, height, weight ) { // Compute BMI if (units == "English") return bmiE(weight, height); else return bmiM(weight, height); } Local to function

Function Features (cont.) Global … declared outside of functions var priceperoz = ;... function worthau ( weight ) { // Compute the dollar value // of weight at priceperoz return weight * 12 * priceperoz; }... var priceperoz = ;... function worthau ( weight ) { // Compute the dollar value // of weight at priceperoz return weight * 12 * priceperoz; }... Global to function

Function Features (cont.) Parameters vs Arguments … parameters are the “formal” variables in the function declaration; arguments are the same thing in the call function BMI (units, height, weight ) { // Compute BMI if (units == "English") return bmiE(weight, height); else return bmiM(weight, height); }... index = BMI("English",72,200);... function BMI (units, height, weight ) { // Compute BMI if (units == "English") return bmiE(weight, height); else return bmiM(weight, height); }... index = BMI("English",72,200);... Parameters Arguments

Summary Functions are packages for algorithms They follow a series of rules, that quickly become intuitive Functions have both a declaration and a call Functions have both parameters (in the declaration) and arguments (in the call) Scope refers to the region of a program where a variable is “known” Functions are the secret to building complex systems