Presentation is loading. Please wait.

Presentation is loading. Please wait.

20061004 chap3 Chapter 3 Top-Down Design with Functions.

Similar presentations


Presentation on theme: "20061004 chap3 Chapter 3 Top-Down Design with Functions."— Presentation transcript:

1 20061004 chap3 Chapter 3 Top-Down Design with Functions

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

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

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

5 20061004 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

6 20061004 chap3 6 Square Root computation

7 20061004 chap3 7 Square Root Program

8 20061004 chap3 8 Square Root Program (cont ’ d)

9 20061004 chap3 9 Some Mathematical Library Functions

10 20061004 chap3 10 Your own functions?

11 20061004 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.

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

13 20061004 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

14 20061004 chap3 14 Structure Chart

15 20061004 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.

16 20061004 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.

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

18 20061004 chap3 18 Function Definitions

19 20061004 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.

20 20061004 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.

21 20061004 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)

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

23 20061004 chap3 23 Functions with Input Arguments and A Single Result.

24 20061004 chap3 24 Example: Single Input & Single Output

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

26 20061004 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.

27 20061004 chap3 27 The Function Data Area

28 20061004 chap3 28 The Function Data Area

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

30 20061004 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


Download ppt "20061004 chap3 Chapter 3 Top-Down Design with Functions."

Similar presentations


Ads by Google