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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

User Defined Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
Functions Jay Summet. Aug Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)
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:
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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
JavaScript with Input & Output Step 1: Use tags JavaScript Template.
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.
CH07: Writing the Programs Does not teach you how to program, but point out some software engineering practices that you should should keep in mind as.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
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.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
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.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
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.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
 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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
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.
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.
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
Topic: Functions – Part 2
Organization of Programming Languages
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 1 of 3 Topics Using Predefined Functions
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 6: User-Defined Functions I
Functions Jay Summet.
Functions, Part 1 of 3 Topics Using Predefined Functions
Flow of Control.
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.
 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.
Functions Jay Summet.
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

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

2 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.

3 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.

4 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.

5 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

6 Sample Programmer-Defined Function Function Example <!-- function PrintMessage() { alert("A message for you:\n\nHave a nice day!"); } //--> <!-- PrintMessage(); //-->

7 Screenshot of Function Example

8 Examining PrintMessage() Function Example <!-- function PrintMessage() { alert("A message for you:\n\nHave a nice day!"); } //--> <!-- PrintMessage(); //--> Function Call Function Definition

9 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!"); } <!-- PrintMessage(); //--> Same name and no arguments (nothing inside of the parentheses)

10 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.

11 General Function Definition Syntax function FunctionName ( parameter 1,..., parameter n ) { 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.

12 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.

13 Function Parameter Example <!-- function PrintMessage(counter) { var i; for(i = 1; i <= counter; i = i + 1) { alert("Have a nice day!"); } //--> <!-- var counter; counter = prompt("Enter a number:"); PrintMessage(counter); //-->

14 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

15 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 = i + 1) { alert("Have a nice day!"); }