1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.

Slides:



Advertisements
Similar presentations
1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
Advertisements

JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Page 1 of 26 Javascript/Jscript Ch 7,8,9,10 Vadim Parizher Computer Science Department California State University, Northridge Spring 2003 Slides from.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
1 JavaScript/Jscript 4 Functions. 2 Introduction Programs that solve real-world programs –More complex than programs from previous chapters Best way to.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Computers and Scientific Thinking David Reed, Creighton University Functions and Randomness 1.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 11 – JavaScript/Jscript: Functions Outline 11.1Introduction 11.2Program Modules in JavaScript.
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
1 Lecture 3 Part 1 Functions with math and randomness.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Pemrograman Teknologi Internet W06: Functions and Events.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
INE1020: Introduction to Internet Engineering 3: Web Page Authoring1 Lecture 5: Introduction to Javascript I r Java and Javascript r Intro. to Javascript.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved. 2 Revised by Dr. T. Tran for CSI3140.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
JavaScript, Fourth Edition
Chapter 10: JavaScript Functions CIS 275—Web Application Development for Business I.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing a Line of Text in a Web Page 7.3 Another JavaScript.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
REEM ALMOTIRI Information Technology Department Majmaah University.
Functions Comp 205 Fall ‘17.
JavaScript: Functions
JavaScript Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 10 - JavaScript: Functions
Chapter 5 - Functions Outline 5.1 Introduction
JavaScript: Functions
Chapter 6 - Functions Outline 5.1 Introduction
JavaScript: Functions
Chapter 10 - JavaScript: Functions
Presentation transcript:

1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in which defined Most functions have list of parameters Means for communicating info between functions & function calls Local variables When function called, arguments assigned to parameters in function definition

2 Programmer-Defined Functions Motives for modularizing a program with functions 1. Makes program development more manageable 2. Allows software reusability Programs can be created from standard functions instead of being built from customized code Example: parseInt(), parseFloat Functions should be limited to performing a single, well-defined task 3. Avoid repeating code in program Do not re-invent the wheel Save time

3 Function Definitions Function-definition format function function-name ( parameter-list ) { Declarations and Statements } Function name - any valid identifier Parameter list - comma-separated list containing names of parameters received by the function when it is called If function does not receive values, parameter-list is left empty

4 Function Definitions Function body or block: declarations and statements within braces Control Returned to the point at which function was called If function does not return a result 1. When right-brace is reached return statement is executed If function returns a result 3. When return expression; is executed Returns value of expressions to caller One argument in function call for each parameter in function definition

A Programmer-Defined square Function document.writeln( 10 " Square the numbers from 1 to 10 " ); // square the numbers from 1 to for ( var x = 1; x <= 10; ++x ) 14 document.writeln( "The square of " + x + " is " + 15 square( x ) + " " ); // The following square function's body is executed only 18 // when the function is explicitly called // square function definition 21 function square( y ) 22 { 23 return y * y; 24 }

6 Script Output

7 Function Definitions Method Math.max( y, z ) Returns larger of the two values inputted When writing a function, do not Forget to return a value if function is supposed to return a value Forget to surround the function body with braces Pass an argument to function that is not compatible with expected data type

Finding the Maximum of Three Values var input1 = window.prompt( "Enter first number", "0" ); 10 var input2 = window.prompt( "Enter second number", "0" ); 11 var input3 = window.prompt( "Enter third number", "0" ); var value1 = parseFloat( input1 ); 14 var value2 = parseFloat( input2 ); 15 var value3 = parseFloat( input3 ); var maxValue = maximum( value1, value2, value3 ); document.writeln( "First number: " + value " Second number: " + value " Third number: " + value " Maximum is: " + maxValue ); // maximum method definition (called from line 17) 25 function maximum( x, y, z ) 26 { 27 return Math.max( x, Math.max( y, z ) ); 28 } 29

Click Refresh (or Reload) to run the script again User Input

10 Script Output

11 Random Number Generation Commonly used in simulations and gaming Method Math.random Returns floating-point value between 0 and 1, inclusive Every value in the range has an equal chance (or probability) of being chosen each time random called Math.floor( argument ); Rounds down the argument to the next integer

12 Random Number Generation Format for range of consecutive integers: For a value in a specific range of consecutive integers, use following format: Math.floor( a + Math.random() * b ); a is the shifting value Equal to the first number in the desired range b is the scaling factor Equal to the width of the desired range Also possible to choose from sets of values other than ranges of consecutive integers

Shifted and Scaled Random Integers var value; document.writeln( " Random Numbers " + 12 " " ); for ( var i = 1; i <= 20; i++ ) { 15 value = Math.floor( 1 + Math.random() * 6 ); 16 document.writeln( " " + value + " " ); if ( i % 5 == 0 && i != 20 ) 19 document.writeln( " " ); 20 } document.writeln( " " ); Click Refresh (or Reload) to run the script again 28 29

14 Script Outputs

Roll a Six-Sided Die 6000 Times var frequency1 = 0, frequency2 = 0, 10 frequency3 = 0, frequency4 = 0, 11 frequency5 = 0, frequency6 = 0, face; // summarize results 14 for ( var roll = 1; roll <= 6000; ++roll ) { 15 face = Math.floor( 1 + Math.random() * 6 ); switch ( face ) { 18 case 1: 19 ++frequency1; 20 break; 21 case 2: 22 ++frequency2; 23 break; 24 case 3: 25 ++frequency3; 26 break; 27 case 4: 28 ++frequency4; 29 break; 30 case 5: 31 ++frequency5; 32 break; 33 case 6:

frequency6; 35 break; 36 } 37 } document.writeln( " " ); 40 document.writeln( " Face " + 41 " Frequency " ); 42 document.writeln( " 1 " + frequency " " ); 44 document.writeln( " 2 " + frequency " " ); 46 document.writeln( " 3 " + frequency " " ); 48 document.writeln( " 4 " + frequency " " ); 50 document.writeln( " 5 " + frequency " " ); 52 document.writeln( " 6 " + frequency " " ); Click Refresh (or Reload) to run the script again 59 60

17 Script Output from First Execution

18 Script Output from Second Execution

19 A Game of Chance Program can also receive input from user through forms GUI - Graphical User Interface Any user interaction with a GUI is called an event Event handling – JavaScript execution in response to an event GUI ’ s are located in the BODY of the HTML document

20 A Game of Chance GUI Setup: GUI is enclosed inside an HTML Form … tags Every GUI output is defined with the INPUT element Enter as many tags as needed Clicking on GUI button element causes an action Function indicated executed when button clicked

21 Example:A Game of Chance Output data to form elements Within a function, write a statement in the following format: formName.inputName.value = variableToBeOutput; Browser status bar Print text by typing window.status = “text to be printed”; GUI ’ s can also be used for user input (discussed in 11.10)

22 Duration of Identifiers Each identifier has duration and scope Duration (or lifetime) is the period during which identifier exists in memory. Some identifiers exist briefly Some identifiers are repeatedly created and destroyed Some identifiers exist for entire execution of the program Identifiers which represent local variables in a function have automatic duration Automatically created when program control enters function Exist while function is active Automatically destroyed when function is exited Referred to as local variables

23 Duration of Identifiers JavaScript also has identifiers of static duration Typically defined in section of HTML document Exist from point at which declared until browsing session over Even though they exist after section terminates, cannot necessarily be used throughout the script Referred to as global variables or script-level variables

24 JavaScript Global Functions Global functions are part of JavaScript ’ s Global object Contains all global variables in the script Some programmers refer to these functions as methods Global functions and user-defined functions part of Global object You do not need to use the Global object directly JavaScript does this for you

25 JavaScript Global Functions

26 JavaScript Global Functions Continued from previous slide