Keeping it Neat: Functions and JavaScript Source Files Chapter 7.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Computer Programming w/ Eng. Applications
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
An Introduction to Programming with C++ Fifth Edition
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
VBA Modules, Functions, Variables, and Constants
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Data types and variables
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Basic Elements of C++ Chapter 2.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Functions. Program complexity the more complicated our programs get, the more difficult they are to develop and debug. It is easier to write short algorithms.
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Objectives You should be able to describe: Data Types
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Flow of Control Part 1: Selection
CPS120: Introduction to Computer Science Decision Making in Programs.
Introduction to Programming with RAPTOR
CPS120: Introduction to Computer Science Functions.
David Stotts Computer Science Department UNC Chapel Hill.
Basic Data Types Numbers (integer and floating point)‏ Strings (sequences of characters)‏ Boolean values (true/false)‏
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 5 JavaScript.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
JavaScript, Fourth Edition
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CSCE 102 – Chapter 11 (Performing Calculations) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
XP Tutorial 2 New Perspectives on JavaScript, Comprehensive1 Working with Operators and Expressions Creating a New Year’s Day Countdown Clock.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Controlling Program Flow with Decision Structures.
Tutorial 11 1 JavaScript Operators and Expressions.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Expressions and Data Types Professor Robin Burke.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
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.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Chapter 9: Value-Returning Functions
A variable is a name for a value stored in memory.
User-Written Functions
Chapter 6 JavaScript: Introduction to Scripting
Programming the Web using XHTML and JavaScript
The Selection Structure
User-Defined Functions
PHP.
JavaScript: Introduction to Scripting
Chapter 5 JavaScript Numbers and Expressions
Presentation transcript:

Keeping it Neat: Functions and JavaScript Source Files Chapter 7

7.1 Functions

Built-In Functions Built-in functions are like subprograms They contain one or more parameters (a value to be sent to the function) They return (send back to the program) at least one value When a built-in function is called, the function name is assigned a value (of the type specified for that function) A built-in function is called by using the function name anywhere in the program that a constant of its type is allowed Some JavaScript global functions: FunctionDescription isFinite()determines whether a value is a finite, legal number isNan()determines whether a value is an illegal number parseFloat()returns a floating point number parseInt()returns an integer String()converts an object's value to a string

User-Defined Functions Built-in functions return a single value to the program, such as: age = parseInt(34.56); age will have the value 34 after the statement is executed You set the value of a variable ( age ) equal to the result of calling the function when the value you send it is We can create that type of function ourselves: a user-defined function Example: The output, after clicking a button to call clickit(), will be 9, the sum of 4 and 5

7.2 The Scope of a Variable

Global vs Local Variables When a variable is input, processed, or output, we say that it has been referenced. The scope of a variable refers to the part of the program in which a given variable can be referenced. Global scope: If a variable is defined outside of all functions then the variable will exist from that point until the end of the program. Local scope: any variable defined within a function will only exist while the function is running.

Beware the Global Variable!

Results If 105 is entered for your grandmother's age and 3 for your puppy's age, output would look as shown. The value of age on line 6 is 105 It is changed on line 10 to 3 Since age has global scope within the getAges() function, it displays this value as requested on line 11 It retains this value even after the pet() function is exited This is what is displayed when line 13 is executed.

Using Local Variables The problem caused by a global variable can be fixed. Changing the line from the previous program: age = parseInt(prompt("How old is your puppy?")); to: var age = parseInt(prompt("How old is your puppy?")); This fixes the problem! It creates a new variable, albeit with the same name ( age ) which is only local to the pet() function.

7.3 Sending Information to a Function

Arguments and Parameters Functions often need to receive information from the statement that calls them. These values, sent to a function, are arguments. The values in the parentheses in a function’s name are parameters. Arguments can be variables, expressions, or values. Parameters must be variables. When sending an argument to a function, it must be the same data type as the parameter that receives it.

Passing Arguments to Parameters The variables, name and age are the arguments that are passed to the parameters in the function newAge(). Note that the string variable, name, must be passed to the variable person and the numeric variable, age, must be passed to the variable, num1. The output will be: – Lizzy, next century you’ll be 125! If line 4 was written as: newAge(age, name); the output would be: – 25, next century you’ll be NaN!

The return Statement The return statement allows functions to send a value back to the expression or statement that called them. A return statement can only return a single value. This program takes the value of age (sent to the function) and returns a new value which is then used in the output.

Passing Values When a variable is declared, a location in the computer's memory is identified. When the name of that variable is used in the program, the computer goes to that location to retrieve the value. If a variable named cars is assigned the value of 12 and a new variable, named trucks and is set equal to cars, then trucks has the value of 12 also. Now two areas in the computer have the value of 12—one referenced by cars and the other referenced by trucks. Anything done to cars to change its value will not affect the value of trucks. This is the process that happens when a variable is passed by value. – The function that received the variable makes a new copy of that variable and manipulates the new copy. That's one way to pass variables in a program. When a variable is passed by reference, the function receives the location in the computer's memory of the variable. The variable in the function probably has a different name but the new variable points to the same location as the original variable. – From that point on, any changes made to the new variable change the value in the location and, therefore, change the original variable as well.

Value and Reference Parameters Value parameters: Changes to their values in the function do not affect the value of the corresponding (argument) variables in the calling function. These parameters can only be used to import data. Reference parameters: Changes in their values do affect the corresponding arguments in the calling function. They can be used to import data into and export data from a function. In JavaScript, primitive types are manipulated by value – numbers, booleans, and strings are considered primitive types in JavaScript reference types are manipulated by reference – objects are considered reference types – arrays and functions, which are specialized types of objects, are also reference types

7.4 Objects and Object- Oriented Concepts

The Math Object Properties and Methods Some Properties of the Math Object PropertyDescription LN10 returns the natural logarithm of 10 (  2.302) PI E returns Euler's number (  2.718) SQRT2 SQRT1_2 Some Methods of the Math Object MethodDescription abs(x)returns the absolute value of x floor(x)returns the integer value of x, rounded down random()returns a random number between 0 and 1 pow(x,y)returns the value x y round(x)rounds x to the nearest integer sqrt(x)returns the square root of x

More JavaScript Objects JavaScript ObjectDescription Arrayallows you to store multiple values with a single variable name Booleanallows you to convert a non-Boolean value to a Boolean (true/false) value Dateused to work with dates and times Mathallows you to perform mathematical tasks Numberused for primitive numeric values Stringallows you to manipulate and store text

The Boolean Object The program on the next slide creates four variables ( one, two, three, and four ) with various values. Four instances of the Boolean object are created. The values of each of the four variables are sent to the function that creates a new instance of the Boolean object. The object turns the value into a Boolean value. – Since variables are passed by reference, the value of the initial variable remains the same. If this program is coded and run the output will show that one still retains its initial value even though the Boolean object, bool1, now has the value of false. The output shows that a 0 is a Boolean false, a 1 is a Boolean true, an underscore is a Boolean true, and NaN is a Boolean false.

Boolean Objects

The Date Object Some Methods of the Date Object Some Methods of the Date() Object MethodDescription getDate()returns the day of the month (numerical, from 1 to 31) getDay()returns the day of the week (from 0 to 6) getFullYear()returns the year in four digits getHours()returns the hour (from 0 to 23) getMinutes()returns the minutes (from 0 to 59) getMonth()returns the month (from 0 to 11) getTime()returns the number of milliseconds since midnight January 1, 1970 getTimezoneOffset()returns the time difference, in minutes, between the local time and Greenwich Mean time (GMT) setDate()sets the day of the month of a Date object setFullYear()sets the year, using four digits, of a Date object setHours()sets the hour of a Date object setMonth()sets the month of a Date object setTime() sets a date and time by adding or subtracting a number of milliseconds that you specify to or from midnight, January 1, 1970 toString()converts a Date object to a string toTimeString()converts the time portion of a Date object to a string

Using the setTimeout() Function to Create a Clock

7.5 JavaScript Source Files

Creating a Source File Comparable to an external css file Is a simple text file containing JavaScript functions Use the.js extension Use to create a library of functions Link to the file in either or :

Creating a Source File This example creates a source file which we will call mySource.js. It contains one function that will check whether a word is spelled correctly.

Using a Source File Checking if user enters a valid code

What happens? shipIt() is called when the button is clicked. – it initializes a variable with the value of the free shipping code ( "FREEBIE" ) – then prompts the user to enter his or her code – then it calls a function named checkWord(). If the checkWord() function returns true, line 10 is executed. If checkWord() returns false, the else statement on line 12 is executed. When checkWord() is called, the computer will look through the section for a function named checkWord(). Since none is found, it goes to the.js file and looks for checkWord() there. The variables, shipCode and userCode, are passed into the parameters of checkWord() which are x and y. The function evaluates x and y, checking to see if they match. – If they don't, the variable spell is changed to false – if they do, spell remains as it was initialized, true. – This is returned to the shipIt() function and the program continues.