20061004 chap3 Chapter 3 Top-Down Design with Functions.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 3 Top-Down Design with Functions. 3-2 Outline 3.1 BUILDING PROGRAMS FROM EXISING INFORMATION –CASE STUDY: FINDING THE AREA AND CIRCUMFERENCE OF.
Chapter 3 Top-Down Design with Functions Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Top-Down Design with Functions Problem Solving and Program Design.
Chapter 3: Modularization
Programming Logic and Design Fourth Edition, Introductory
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
CS 201 Functions Debzani Deb.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Chapter 6: User-Defined Functions I
: The Scrooge Co Problem ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11047: The Scrooge Co Problem 解題者:鍾正一 解題日期: 2007 年 6 月 5 日 題意:輸入 X.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Guide To UNIX Using Linux Third Edition
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
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.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
1 Introduction to Computers II Lecture 4 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
chap7 Chapter 7 Simple Data Types chap7 2 Objectives No programming language can predefine all the data types that a programmer may.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 3 Top-Down Design with Functions and Classes Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
chap4 Chapter 4 Selection Structures: if and switch Statements.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Chapter 3 Top-Down Design with Functions. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-2 Figure 3.1 Edited Data Requirements and Algorithm.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
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.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
Chapter 6: User-Defined Functions I
Function Topic 4.
About the Presentations
User-Defined Functions
Functions, Procedures, and Abstraction
Lec8.
Chapter 1 Introduction(1.1)
Top-Down Design with Functions
Top-Down Design with Functions
Agenda for Unit 3: Functions
Presentation transcript:

chap3 Chapter 3 Top-Down Design with Functions

chap3 2 Objectives Building Programs from Existing functions Some Mathematical Library Functions Create you own functions Top-Down Design & Structure Charts Declare Functions Order of Execution of Function Subprograms and Main function The Function Data Area

chap3 3 Building Programs from Existing Information Programmer seldom start off with an empty program Reasons: Most basic or frequently used functions have been written by other programmers If you write every function by yourself, the source code may be messy and hard to maintain Less code, less bugs Deadline Many existing libraries have tuned the performance, and are more reliable and robust.

chap3 4 Conversion Program We have used some pre-written I/O functions provided in C

chap3 5 Predefined Functions and Code Reuse A primary goal of software engineering is to write error-free code. Code reuse: reusing program fragments that have been written and tested. C library functions provide most commonly used functions. e.g., mathematical library To use existing C library functions, we have to include the header file of the corresponding library. e.g., #include

chap3 6 Square Root computation

chap3 7 Square Root Program

chap3 8 Square Root Program (cont ’ d)

chap3 9 Some Mathematical Library Functions

chap3 10 Your own functions?

chap3 11 Top-Down Design & Structure Charts Most likely a problem is complex, and we have to break up the problem into subproblems. Top-Down Design is a method in which we break a problem into subproblems, and derive the solution for the original problem. Structure Chart is a documentation tool that shows the relationships among the subproblems of a problem.

chap3 12 Example: Draw Simple Diagrams House and Female Stick Figure

chap3 13 House and Female Stick Figure The house consists of a triangle without its base and a rectangle. The female stick figure consists of a circle, a triangle, and a triangle without its base. Suppose we are given four basic drawing components (functions). A circle Parallel lines A base line Intersecting lines

chap3 14 Structure Chart

chap3 15 Use and define your own functions One way to implement top-down design is by defining your own functions. Usually a subproblem is solved by a corresponding subproblem. Functions without Arguments Simple Functions that have no arguments and return no value.

chap3 16 Declare Functions without Arguments Declaration: ftype fname(void); The identifier ftype specifies the data type of the function results. Example: void fname(void); After fname() is called, the execution of the program jumps to the subprogram defined by fname.

chap3 17 Example: Function Prototypes & Main function for Stick Figure

chap3 18 Function Definitions

chap3 19 Order of Execution of Function Subprograms and Main function 1. When the computer executes a function call statement, it transfers the control to the function. 2. The computer allocates memory for variables used in the function and executes the statements in the function body. 3. After the execution of the function, the control is returned to the calling function and the allocated memory is released.

chap3 20 Advantages of Using Function Subprograms Procedural Abstraction The main function consists of a sequence of function calls. We defer implementation details until we are ready to write the subprogram. We can focus on one function at a time. Reuse of Function Subprograms The subprogram can be executed more than once in a program Decrease the length of code and chance of error.

chap3 21 Function with Arguments The arguments of a function are used to transfer information between the calling and called functions. Input Arguments are used to pass information into a subprogram from the calling function. (in this chapter) Output Arguments are used to return results to the calling function. (in Chap 6)

chap3 22 Void Functions with Input Arguments We can create functions that receive input arguments but do not return any results.

chap3 23 Functions with Input Arguments and A Single Result.

chap3 24 Example: Single Input & Single Output

chap3 25 Effect of Executing circum = find_circum (radius);

chap3 26 Example: Multiple Input Arguments & Single Output Function scale multiples the first input argument by 10 raised to the power indicated by the second input argument.

chap3 27 The Function Data Area

chap3 28 The Function Data Area

chap3 29 Homework #3 Programming Project 4 (Textbook. Page 141) due: 2006/10/11 讓使用者輸入一個物體發射的仰角、初速、目標所在的距離 將下列兩個計算寫成兩個function 並印出你的計算結果 1. 此物體到達目標的時間 2. 此物體到達目標時離地面的高度

chap3 30 Summary Building Programs from Existing functions Some Mathematical Library Functions Create you own functions Top-Down Design & Structure Charts Declare Functions Order of Execution of Function Subprograms and Main function The Function Data Area