20061004 chap3 chapter 3 top-down design with functions
TRANSCRIPT
20061004 chap3
Chapter 3
Top-Down Design with Functions
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
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.
20061004 chap3 4
Conversion Program We have used some pre-written I/O
functions provided in C
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 <math.h>
To use existing C library functions, we have to include the header file of the corresponding library. e.g., #include <math.h>
20061004 chap3 6
Square Root computation
20061004 chap3 7
Square Root Program
20061004 chap3 8
Square Root Program (cont’d)
20061004 chap3 9
Some Mathematical Library Functions
20061004 chap3 10
Your own functions?
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.
20061004 chap3 12
Example: Draw Simple Diagrams
House and Female Stick Figure
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
20061004 chap3 14
Structure Chart
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.
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.
20061004 chap3 17
Example: Function Prototypes & Main function for Stick Figure
20061004 chap3 18
Function Definitions
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.
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.
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)
20061004 chap3 22
Void Functions with Input Arguments We can create functions that receive input
arguments but do not return any results.
20061004 chap3 23
Functions with Input Arguments and A Single Result.
20061004 chap3 24
Example: Single Input & Single Output
20061004 chap3 25
Effect of Executing circum = find_circum (radius);
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.
20061004 chap3 27
The Function Data Area
20061004 chap3 28
The Function Data Area
20061004 chap3 29
Homework #3 Programming Project 4 (Textbook. Page 141)
due: 2006/10/11
讓使用者輸入一個物體發射的仰角、初速、目標所在的距離
將下列兩個計算寫成兩個 function 並印出你的計算結果 1. 此物體到達目標的時間
2. 此物體到達目標時離地面的高度
2
**)sin(*
2timegtimevelocityheight
)cos(*
tan
velocity
cedistime
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