Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

User Defined Functions
Modular Programming With Functions
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Top-Down Design with Functions Problem Solving and Program Design.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
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
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
ICS103 Programming in C Lecture 9: 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 1 Principles of Programming and Software Engineering.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
C++ for Engineers and Scientists Third Edition
1 Introduction to Computers II Lecture 4 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
© 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 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
1 ICS103 Programming in C Ch3: Top-Down Design with Functions.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
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.
chap3 Chapter 3 Top-Down Design with Functions.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
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
CISC105 – General Computer Science Class 2 – 6/7/2006.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
1 ICS103 Programming in C Lecture 9: Functions I.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
Function Topic 4.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
User-Defined Functions
User Defined Functions
Chapter 6 Methods: A Deeper Look
Lec8.
Chapter 6: User-Defined Functions I
Top-Down Design with Functions
Top-Down Design with Functions
Presentation transcript:

Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-2 Functions without Arguments One way that programmers implement top- down design in their programs is by defining their own functions.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-3 Functions without Arguments -- Function Prototypes A function must be declared before it can be referenced. A function prototype tells the C compiler the data type of the function, the function name, and information about the arguments. The data type of a function is determined by the type of value returned by the function. –void function a function that does not return a value

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-4 Functions without Arguments -- Function Prototypes (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-5 Functions without Arguments -- Function Prototypes (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-6 Functions without Arguments -- Function Definitions A definition for each function Similar to the function prototype

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-7 Functions without Arguments -- Function Definitions (Cont’) The function call statement draw_circle(); causes these printf statements to execute. Control returns to the main function after the circle shape is displayed.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-8 Functions without Arguments -- Function Definitions (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-9 Functions without Arguments -- Function Definitions (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-10 Functions without Arguments -- Function Definitions (Cont’) Each function body may contain declarations for its own variables. –These variables are considered local to the function, which can be referenced only within the function.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-11 Functions without Arguments -- Function Definitions (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-12 Functions without Arguments -- Placement of Functions in a Program The subprogram (function) prototypes precede the main function (after any #include or #define directives). The subprogram definitions follow the main function. The relative order of the function definitions does not affect their order of execution, which is determined by the order of execution of the function calls.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-13 Functions without Arguments -- Placement of Functions in a Program (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-14 Functions without Arguments -- Placement of Functions in a Program (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-15 Functions without Arguments -- Order of Execution of Function Subprograms and Main Function The information in each prototype enables the compiler to correctly translate a call to that function. –As a transfer of control to the function –At the end of a function body, inserts a statement that causes a transfer of control back from the function to the calling statement. The computer allocates any memory that may be needed for variables declared in the function at run-time.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-16 Figure 3.15 Flow of Control Between the main Function and a Function Subprogram

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-17 Functions without Arguments -- Advantages of Using Function Subprograms For a team of programmers working together on a large program, subprograms make it easier to apportion programming tasks. Procedural Abstraction –A programming technique in which a main function consists of a sequence of function calls and each function is implemented separately –Focusing on one function at a time is much easier than trying to write the complete program all at once.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-18 Functions without Arguments -- Advantages of Using Function Subprograms (Cont’d) Reuse of Function Subprograms –Functions can be executed more than once in a program. –Without functions, statements would be listed twice in the main function, thereby increasing the main function’s length and the chance of error. –Once you have written and tested a function, you can use it in other programs or functions.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-19 Functions without Arguments -- Displaying User Instructions

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-20 Functions with Input Arguments Functions with input arguments are more like Lego® blocks than the smooth-sided wooden blocks –One surface with little protrusions and one surface with little cups

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-21 Functions with Input Arguments (Cont’) input arguments –arguments used to pass information into a function subprogram –E.g. rim_area = find_area(edge_radius) - find_area(hole_area); output arguments –arguments used to return results to the calling function Arguments make function subprograms more versatile because they enable a function to manipulate different data each time it is called.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-22 Functions with Input Arguments -- Actual arguments and Formal parameter actual argument –an expression used inside the parentheses of a function call formal parameter –an identifier that represents a corresponding actual argument in a function definition

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-23 Figure 3.18 Function print_rboxed and Sample Run

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-24 Functions with Input Arguments -- Example (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-25 Figure 3.20 Function with Input Arguments and One Result

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-26 Figure 3.21 Functions find_circum and find_area

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-27 Figure 3.22 Effect of Executing circum = find_circum (radius);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-28

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-29 Functions with Input Arguments -- Program Style Function Interface Comment The function interface comment combined with the heading (or prototype) provides valuable documentation to other programmers who might want to reuse your functions in a new program without reading the function code. precondition –a condition assumed to be true before a function call postcondition –a condition assumed to be true after a function executes

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-30 Functions with Input Arguments -- Functions with Multiple Arguments

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-31 Functions with Input Arguments -- Functions with Multiple Arguments (Cont’) scale(2.5, 2) returns the value (2.5 × 10 2 ) scale(2.5, -2) returns the value (2.5 × )

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-32 Figure 3.24 Testing Function scale

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-33 Figure 3.24 Testing Function scale (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-34 Functions with Input Arguments -- Argument List Correspondence The number of actual arguments used in a call to a function must be the same as the number of formal parameters listed in the function prototype. The order of arguments in the lists determines correspondence. The first actual argument corresponds to the first formal parameter, the second actual argument corresponds to the second formal parameter, and so on. Each actual argument must be of a data type that can be assigned to the corresponding formal parameter with no unexpected loss of information.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-35 Functions with Input Arguments -- Argument List Correspondence (Cont’) Using an actual argument of the same data type as the corresponding formal parameter is not always essential. –There is no loss of information when an int is assigned to a type double variable.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-36 Functions with Input Arguments -- The Function Data Area Each time a function call is executed, an area of memory is allocated for storage of that function’s data. Included in the function data area are storage cells for its formal parameters and any local variables that may be declared in the function. The function data area is always lost when the function terminates; it is recreated empty (all values undefined) when the function is called again. The local variable can be accessed only in function.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-37 Functions with Input Arguments -- The Function Data Area (Cont’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-38 Functions with Input Arguments -- Testing Functions Using Drivers A function is an independent program module, and as such, it can be tested separately from the program that uses it. driver –a short function written to test another function by defining its arguments, calling it, and displaying its result

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-39 Common Programming Errors use a #include preprocessor directive for every standard library from which you are using functions. Place prototypes for your own function subprograms in the source file preceding the main function. Place the actual function definitions after the main function. The acronym n.o.t. summarizes the requirements for argument list correspondence. Be careful in using functions that are undefined on some range of values.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-40 Chapter Review Develop your program solutions from existing information. Use the system documentation from the software development method. –data requirements –refined algorithm Modify the previous program rather than starting from scratch. Use C’s library functions to simplify mathematical computations through the reuse of code that has already been written and tested. Use a structure chart to show subordinate relationships between subproblems. Utilize modular programming by writing separate function subprograms, and the main function will consist of a sequence of function call statements. Actual argument value is assigned to its corresponding formal parameter. Place prototypes for each function subprogram.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-41 New C Constructs

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3-42 New C Constructs (Cont’)