JavaScript: Functions.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
1 Arrays in JavaScript Name of array (Note that all elements of this array have the same name, c ) Position number of the element within array c c[6] c[0]
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 2006 Pearson Education, Inc. All rights reserved Arrays.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
IE 212: Computational Methods for Industrial Engineering
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Arrays 1.
7.1 Introduction Arrays Arrays are data structures consisting of data items of the same type “Static” entities They remain the same size once they are.
REEM ALMOTIRI Information Technology Department Majmaah University.
Arrays Chapter 7.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Lecture 5 array declaration and instantiation array reference
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Chapter 11 - JavaScript: Arrays
Arrays Chapter 7.
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
Chapter 17 – JavaScript/Jscript: Arrays
© 2016 Pearson Education, Ltd. All rights reserved.
JavaScript: Functions
JavaScript Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 3 Introduction to Classes, Objects Methods and Strings
7 Arrays.
Chapter 6 Methods: A Deeper Look
7 Arrays.
Arrays Kingdom of Saudi Arabia
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
CHAPTER 6 GENERAL-PURPOSE METHODS
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
JavaScript Arrays.
MSIS 655 Advanced Business Applications Programming
Arrays.
Arrays Chapter 7.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
CIS16 Application Development and Programming using Visual Basic.net
7 Arrays.
JavaScript: Arrays.
Object Oriented Programming in java
Arrays Arrays A few types Structures of related data items
10 JavaScript: Arrays.
C++ Array 1.
Arrays.
Presentation transcript:

JavaScript: Functions

Program Modules in JavaScript JavaScript programs are written by combining new functions that the programmer writes with “prepackaged” functions and objects available in JavaScript The term method implies that a function belongs to a particular object We refer to functions that belong to a particular JavaScript object as methods; all others are referred to as functions. JavaScript provides several objects that have a rich collection of methods for performing common mathematical calculations, string manipulations, date and time manipulations, and manipulations of collections of data called arrays.

Program Modules in JavaScript (Cont.) You can define programmer-defined functions that perform specific tasks and use them at many points in a script The actual statements defining the function are written only once and are hidden from other functions Functions are invoked by writing the name of the function, followed by a left parenthesis, followed by a comma- separated list of zero or more arguments, followed by a right parenthesis Methods are called in the same way as functions, but require the name of the object to which the method belongs and a dot preceding the method name Function (and method) arguments may be constants, variables or expressions

Function Definitions return statement passes information from inside a function back to the point in the program where it was called A function must be called explicitly for the code in its body to execute The format of a function definition is function function-name( parameter-list ) { declarations and statements }

Function Definitions (Cont.) Three ways to return control to the point at which a function was invoked Reaching the function-ending right brace Executing the statement return; Executing the statement “return expression;” to return the value of expression to the caller When a return statement executes, control returns immediately to the point at which the function was invoked

Calls function square with x as an argument, which will return the value to be inserted here Begin function square Names the parameter y Returns the value of y * y (the argument squared) to the caller End function square

Creates float values from user input

Calls function maximum with arguments value1, value2 and value3 Variable maxValue stores the return value of the call to maximum Begin function maximum with local variables x, y and z Calls the Math object’s method max to compare the first variable with the maximum of the other two End function maximum

Example: Random Image Generator We can use random number generation to randomly select from a number of images in order to display a random image each time a page loads

Creates an src attribute by concatenating a random integer from 1 to 7 with “.gif\” to reference one of the images 1.gif, 2.gif, 3.gif, 4.gif, 5.gif, 6.gif or 7.gif

Arrays Introduction Arrays Data structures consisting of related data items Sometimes called collections of data items JavaScript arrays “dynamic” entities that can change size after they are created

Arrays (Cont.) The first element in every array is the zeroth element. The ith element of array c is referred to as c[i-1]. Array names follow the same conventions as other identifiers A subscripted array name can be used on the left side of an assignment to place a new value into an array element can be used on the right side of an assignment operation to use its value Every array in JavaScript knows its own length, which it stores in its length attribute and can be found with the expression arrayname.length

Declaring and Allocating Arrays JavaScript arrays are Array objects. Creating new objects using the new operator is known as creating an instance or instantiating an object Operator new is known as the dynamic memory allocation operator

Operator new allocates an Array called n1 with five elements Operator new allocates an empty Array called n2 Zero-based counting used in for loop to set each element’s value equal to its subscript Five elements added and initialized in n2, which dynamically expands

Outputs the subscript and value of every array element in a table

Examples Using Arrays (Cont.) Arrays can be created using a comma-separated initializer list enclosed in square brackets ([]) The array’s size is determined by the number of values in the initializer list The initial values of an array can be specified as arguments in the parentheses following new Array The size of the array is determined by the number of values in parentheses

Creates an array with four elements, all of which are defined Creates an array with four elements, all of which are defined in an initializer list Creates an array with four elements, two of which reserve space for values to be specified later

References and Reference Parameters (Cont.) All objects are passed to functions by reference Arrays are objects in JavaScript, so Arrays are passed to a function by reference a called function can access the elements of the caller’s original Arrays. Name of an array actually a reference to an object that contains the array elements and the length variable

Passing Arrays to Functions Pass an array as an argument to a function Specify the name of the array (a reference to the array) without brackets Although entire arrays are passed by reference, individual numeric and boolean array elements are passed by value exactly as simple numeric and boolean variables are passed Such simple single pieces of data are called scalars, or scalar quantities To pass an array element to a function, use the subscripted name of the element as an argument in the function call join method of an Array Returns a string that contains all of the elements of an array, separated by the string supplied in the function’s argument If an argument is not specified, the empty string is used as the separator

Passes array a to function modifyArray by reference Passes array element a[3] to function modifyElement by value

Creates a string containing all the elements in theArray, separated by “ ” Multiplies each element in theArray by 2, which persists after the function has finished Multiplies the array element by 2, but only for the duration of the function

Multidimensional Arrays To identify a particular two-dimensional multidimensional array element Specify the two subscripts By convention, the first identifies the element’s row, and the second identifies the element’s column In general, an array with m rows and n columns is called an m-by-n array Two-dimensional array element accessed using an element name of the form a[ i ][ j ] a is the name of the array i and j are the subscripts that uniquely identify the row and column Multidimensional arrays are maintained as arrays of arrays

Multidimensional Arrays (Cont.) Multidimensional arrays can be initialized in declarations like a one-dimensional array, with values grouped by row in square brackets The interpreter determines the number of rows by counting the number of sub initializer The interpreter determines the number of columns in each row by counting the number of values in the sub- array that initializes the row The rows of a two-dimensional array can vary in length A multidimensional array in which each row has a different number of columns can be allocated dynamically with operator new

Initializes array1 with an initializer list of sub initializer lists Initializes array2 with rows of different lengths Nested for…in statements traverse the arrays by iterating through the sets of one-dimensional arrays, then through the elements of each of those one-dimensional arrays