Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.

Similar presentations


Presentation on theme: "Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ."— Presentation transcript:

1 Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ. http://ant.comm.ccu.edu.tw jypan@comm.ccu.edu.tw

2 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-2 Outline Building a program –Case Study Finding Area/Circumference of a circle Top-Down Design and Structure Charts –Case Study: Drawing Simple Diagrams Code reuse and functions

3 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-3 Building Programs from Existing Information (Design  Implementation) Programs = Data structures + Algorithms How to implement a program? You can… –editing data requirements conform to C syntax for constant macro and variable declarations (Data structures) –use the initial algorithm and refinements as program comments, and then write C statements for them (Algorithms)

4 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-4 Case Study Finding the Area and Circumference of a circle(1/4) Step 1: Problem –Get the radius of a circle. Compute and display the circle’s area and circumference.

5 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-5 Case Study Finding the Area and Circumference of a circle(2/4) Step 2: Analysis –Problem Constant PI 3.14159 –Problem Inputs radius –Problem Outputs area circum –Relevant Formulas Area of a circle = π x radius 2 Circumference of a circle = 2 π x radius

6 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-6 Case Study Finding the Area and Circumference of a circle(3/4) Step 3: Design –Initial Algorithm 1. Get the circle radius 2. Calculate the area 3. Calculate the circumference 4. Display the area and the circumference –Algorithm Refinement 2.1 Assign PI * radius * radius to area 3.1 Assign 2 * PI * radius to circum

7 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-7 Case Study Finding the Area and Circumference of a circle(4/4) Step 4: Implementation (Figure 3.2 、 Figure 3.3) Step 5: Testing

8 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-8 Outline of Program calculating Circle

9 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-9 Calculating Area and Circumference of a Circle

10 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-10 Top-Down Design and Structure Charts When a problem is more complex than those we have seen so far… –Must break up the problem into subproblems to develop the program solution, and so on… –Top-down design –A small problem is easier to solve A picture is worth a thousand words –Structure chart –a documentation tool that shows the relationship among the subproblems of a problem

11 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-11 Case Study: Drawing Simple Diagrams(1/2) Step 1: Problem –You want to draw some simple diagrams on your printer or screen. For example, a female stick figure in Fig.3.9. Step 2: Analysis –three basic components a circle a base line Intersecting lines

12 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-12 Case Study: Drawing Simple Diagrams(2/2) Step 3: Design (Figure 3.10) –Initial algorithm 1. Draw a circle. 2. Draw a triangle. 3. Draw intersecting lines. –Refinements 2.1 Draw intersecting lines. 2.2 Draw a base.

13 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-13 Figure 3.10 Structure Chart for Drawing a Stick Figure

14 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-14 Figure 3.11 Function Prototypes and Main Function for Stick Figure

15 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-15 Figure 3.12 Function draw_circle

16 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-16 Figure 3.13 Function draw_triangle

17 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-17 Figure 3.14 Whole Program to Draw a Stick Figure

18 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-18 Figure 3.14 Program to Draw a Stick Figure (cont’d)

19 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-19 Execution order of subprograms and main Figure 3.15 Flow of Control Between the main Function and a Function Subprogram 2. Allocate memory for variables declared 5. Release memory 1. When executes function call statement, it transfer control to referenced function. 3. Execute statements in subprogram function 4. Return control to the main function6. Execute next statement in the main function

20 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-20 Code reuse and functions Why reinvent the wheel? C provides predefined functions –Standard library Appendix B shows ANSI C standard libraries –P.800~801, stdio.h 所列之 functions –P.799, math.h 所列之 functions –P.802, string.h 所列之 functions ( 教到字串時請自學 ) – 自行參考其功用,大略記一下 y = sqrt(x); function call function nameargument Function sqrt as a “Black Box”

21 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-21 Advantages of Using Function Subprograms Easier to apportion( 分配 ) programming tasks in a team Procedure abstraction –a programming technique in which a main function consists of a sequence of function calls and each function is implemented separately –Defer implementation details (How to do) from a subproblem (What to do). –Focusing on one function at a time is much easier Reuse of function subprograms –Functions can be executed more than once –Functions can be used in other programs or functions

22 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-22 Functions with Input/output Arguments Lego blocks “protrusions” and “cups” Input arguments –Arguments used to pass information into a function subprogram Output arguments –Arguments used to return results to the calling function

23 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-23 Testing Function scale 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

24 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-24 Argument List Correspondence (The not rules) 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. Each actual argument must be of a data type that can be assigned to the corresponding formal parameter.

25 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-25 The Function Data Area When a function call is executed… –a memory area is allocated for storage of function data (ex. formal parameter, local var) When function terminates… –function data area is always lost Again? Local variable –initially undefined (usually…)

26 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-26 Data Areas After Call scale(num_1, num_2);

27 中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 3-27 Question? A good question deserve a good grade…


Download ppt "Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ."

Similar presentations


Ads by Google