Functions, Part 1 of 2 Topics Using Predefined Functions

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Static Methods Chapter 4. Chapter Contents Objectives 4.1 Introductory Example: Old MacDonald Had a Farm … 4.2 Getting Started with Methods 4.3 Example:
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
11/1/06 1 Hofstra University, CSC005 Chapter 8 (Part 3) High Level Programming Languages.
Chapter 6: User-Defined Functions I
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Introduction to scripting
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions.
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
Java Script User Defined Functions. Java Script  You can define your own JavaScript functions. Such functions are called user- defined, as opposed to.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript, Fourth Edition
Chapter 10: JavaScript Functions CIS 275—Web Application Development for Business I.
1 Functions, Part 1 of 2 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CS0004: Introduction to Programming
REEM ALMOTIRI Information Technology Department Majmaah University.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 9: Value-Returning Functions
Topic: Functions – Part 1
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6 JavaScript: Introduction to Scripting
Chapter 6: User-Defined Functions I
Functions Comp 205 Fall ‘17.
Topic: Functions – Part 2
Organization of Programming Languages
Introduction to Scripting
JavaScript Syntax and Semantics
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
DHTML.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
User Defined Functions
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions, Part 1 of 3 Topics Using Predefined Functions
Subprograms and Programmer Defined Data Type
Introduction to Classes and Objects
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 6: User-Defined Functions I
CHAPTER 17 The Report Writer Module
Topics Introduction to Functions Defining and Calling a Function
Functions, Part 1 of 3 Topics Using Predefined Functions
JavaScript: Introduction to Scripting
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Methods/Functions.
Functions I Creating a programming with small logical units of code.
CPS125.
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Presentation transcript:

Functions, Part 1 of 2 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments

Review of Structured Programming Structured programming is a problem solving strategy and a programming methodology that includes the following guidelines: The program uses only the sequence, selection, and repetition control structures. The flow of control in the program should be as simple as possible. The construction of a program embodies top-down design.

Functions When program control encounters a function name, the function is called (invoked). Program control passes to the function. The function is executed. Control is passed back to the place where the function was called.

Functions We have used several predefined functions so far: alert() prompt() document.write() toFixed() parseInt() parseFloat() Programmers can write their own functions. Typically, each module in a program’s design hierarchy chart is implemented as a function.

Sample Function Call alert is the name of a predefined function in the JavaScript language alert(“Hello World!”); this statement is is known as a function call this is a string we are passing as an argument (parameter) to the alert function

Sample Programmer-Defined Function <head> <title>Function Example</title> <script type="text/javascript"> <!-- function PrintMessage() { alert(“A message for you:\n\nHave a nice day!”); } //--> </script> </head> <body> PrintMessage(); </body>

Screenshot of Function Example

Examining PrintMessage() <head> <title>Function Example</title> <script type="text/javascript"> <!-- function PrintMessage() { alert(“A message for you:\n\nHave a nice day!”); } //--> </script> </head> <body> PrintMessage(); </body> Function Definition Function Call

The Function Call Passes program control to the function Must match the definition in name and number of arguments ........ function PrintMessage() { alert(“A message for you:\n\nHave a nice day!”); } <body> <script type="text/javascript"> <!-- PrintMessage(); //--> </script> </body> Same name and no arguments (nothing inside of the parentheses)

The Function Definition Control is passed to the function by the function call. The statements within the function body will then be executed. function PrintMessage() { alert(“A message for you:\n\nHave a nice day!”); } After the statements in the function have completed, control is passed back to the place where the function was called.

General Function Definition Syntax function FunctionName ( parameter1, . . . , parametern ) { variable declaration(s) statement(s) } If there are no parameters, there should be nothing inside of the ()'s function FunctionName() ... There may be no variable declarations.

Using Input Parameters Often it is the case that we would like to be able to share information with the function. It is possible to send input parameters into the function. We can pass information from the place where the function is called. The next slide illustrates sending a single parameter into a function.

<title>Function Parameter Example</title> <head> <title>Function Parameter Example</title> <script type="text/javascript"> <!-- function PrintMessage(counter) { var i; for(i = 1; i <= counter; i++) alert(“Have a nice day!”); } //--> </script> </head> <body> var counter; counter = prompt("Enter a number:"); PrintMessage(counter); </body> 13

Good Programming Practice You should include a function header comment before the definition of each function. This is a good practice and is required by the 104 Coding Standards. Your header comments should be neatly formatted and contain the following information: function name function description (what it does) a list of any input parameters and their meanings a list of any output parameters and their meanings a description of any special conditions

Example of a Function Header Comment /*************************************************************** ** PrintMessage - prints a message a specified number of times ** Inputs: counter - the number of times the message will be ** printed ** Outputs: None ***************************************************************/ function PrintMessage(counter) { var i; for(i = 1; i <= counter; i++) alert(“Have a nice day!”); }