Presentation is loading. Please wait.

Presentation is loading. Please wait.

Top-Down Design with Functions

Similar presentations


Presentation on theme: "Top-Down Design with Functions"— Presentation transcript:

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

2 Outline Building a program Top-Down Design and Structure Charts
Case Study Finding Area/Circumference of a circle Top-Down Design and Structure Charts Case Study: Drawing Simple Diagrams Code reuse and functions

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 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 Case Study Finding the Area and Circumference of a circle(2/4)
Step 2: Analysis Problem Constant PI Problem Inputs radius Problem Outputs area circum Relevant Formulas Area of a circle = π x radius2 Circumference of a circle = 2 π x radius

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 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 Outline of Program calculating Circle

9 Calculating Area and Circumference of a Circle

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 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 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 Figure 3.10 Structure Chart for Drawing a Stick Figure

14 Figure 3.11 Function Prototypes and Main Function for Stick Figure

15 Figure 3.12 Function draw_circle

16 Figure 3.13 Function draw_triangle

17 Figure 3.14 Whole Program to Draw a Stick Figure

18 Figure 3.14 Program to Draw a Stick Figure (cont’d)

19 Execution order of subprograms and main Figure 3
Execution order of subprograms and main Figure Flow of Control Between the main Function and a Function Subprogram 2. Allocate memory for variables declared 1. When executes function call statement, it transfer control to referenced function. 3. Execute statements in subprogram function 6. Execute next statement in the main function 4. Return control to the main function 5. Release memory

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 (教到字串時請自學) 自行參考其功用,大略記一下 function call y = sqrt(x); Function sqrt as a “Black Box” function name argument

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 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 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 Testing Function scale

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 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 Data Areas After Call scale(num_1, num_2);

27 Question? A good question deserve a good grade…


Download ppt "Top-Down Design with Functions"

Similar presentations


Ads by Google