chapter 3 : top down design with functions by suraya alias

37
KK10103 – Computer Programming Chapter 3 : Top Down Design with Functions By Suraya Alias

Upload: theodore-stewart

Post on 06-Jan-2018

237 views

Category:

Documents


1 download

DESCRIPTION

1-3

TRANSCRIPT

Chapter 3By Suraya Alias
Figure 3.1 Edited Data Requirements and Algorithm for Conversion Program
1-2
1-3
Case study
1) Problem Definition / Requirement
Get the radius of a circle. Compute and display the circle’s area and Circumference.
2) Analysis
Problem constant
PI = 3.14159
Problem Input
Problem Output
Relevant formula
Circumference of a circle = 2π X radius
1-4
2) Calculate the area
3) Calculate the circumference
Step 2 needs refinement
Step 3 needs refinement
1-5
Figure 3.3 Calculating the Area and the Circumference of a Circle
1-6
5) Implementation 6) Testing
Figure 3.3 Calculating the Area and the Circumference of a Circle (cont’d)
1-7
Figure 3.4 Computing the Rim Area of a Flat Washer
1-8
1-9
1-10
Predefined Function and Code Reuse
By reusing predefined functions such as to perform mathematical function is called code reuse
C’s standard math library using
#include <math.h> defines a function named sqrt that perform the square root computation
Example; y = sqrt(x), where
sqrt is the function name, (x) is the argument
Other <math.h> functions are pow(x,y), log(x), exp(x), cos(x), tan(x), sin(x)
1-11
1-12
3) The function result, 4.0 is assigned to y
Figure 3.7 Square Root Program
1-13
1-14
Example
We can use the C function pow(power) and sqrt to compute the roots of a quadratic equation in x of the form
/*compute two roots, root_1 and root_2, for disc > 0.0*/
disc= pow(b,2)-4 * a * c;
root_1=(-b+sqrt(disc))/2*a;
1-16
Top –Down Design
Problem solving method where problems are break into sub problems, then the sub problems are solved to solve the original problem.
Structure Chart
A documentation tool that shows the relationships among the sub problems of a problem
1-17
1-18
Figure 3.10 Structure Chart for Drawing a Stick Figure
1-19
Syntax : fname();
example: draw_circle();
The empty parentheses after the function name indicate that draw_circle requires no argument
Function prototype
A function must be declared before it can be referenced.
One way is to insert a function prototype before the main function
A function prototype tells the C compiler the data type of the function, the function name and the information about the arguments that the function expects
1-20
Form : ftype fname(void);
Example : void draw_circle(void);
Use void as ftype if the function does not return a value
The argument list (void) indicates that the function has no argument
The function prototype must appear before the first call to the function
1-21
Figure 3.11 Function Prototypes and Main Function for Stick Figure
1-22
1-25
Figure 3.14 Program to Draw a Stick Figure. (Placement of Functions in a Program)
1-26
Figure 3.14 Program to Draw a Stick Figure (cont’d)
1-27
Figure 3.15 Flow of Control Between the main Function and a Function Subprogram
1-28
1) Procedural Abstraction
A programming technique in which a main function consists of a sequence of a function calls and each function is implemented separately
2) Reuse of Function subprogram
The function can be executed more than once in a program
Example : function draw_intersect is called twice
Displaying User Instructions
We can use functions without argument to display message, display lines of program output or instruction to user.
1-29
Figure 3.16 Function instruct and the Output Produced by a Call
1-30
Input Arguments
Output Argument
Arguments used to return results to the calling function
We can also return a single result from a function by executing a return statement in the function body
Argument make function subprogram more versatile
1-31
1-33
Actual Argument
function call, (135.68)
argument in a function definition (rnum)
Figure 3.18 Function print_rboxed and Sample Run (cont’d)
1-34
1-35
1-36
1-37
1-38
If PI = 3.14159, radius=10.0 circum = find_circum(radius)
C subtitute the actual argument used in the function call for the formal parameter r
Figure 3.23 Function scale
Pre condition – a condition assumed to be true before a
function call
Post condition – a condition assumed to be true after a
Function executes
1-40
num_1 x
num_2 n
1-41
Argument List Correspondence
The number of actual arguments used in a call of 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, and so on.
Each actual argument must be of a data type that can be assigned to the responding formal parameter with no unexpected loss of information
Testing functions using driver
Driver is a short function written to test another function by defining its arguments, calling it and displaying its result
The main function can act as a driver
1-42
1-43
From the formula (x * scale_factor)
scale_factor = pow(10, n)
New C Constructs
Function Prototype (void function without argument) void star_line (void);
Describes star_line as a function with no result and no argument
Function Prototype (function with argument and a result) double average (int n, double x);
Describes average as function with a type double result, 2 arguments, one type int and one type double
Function Call Statement (void function without argument) star_line();
Calls function star_line and causes it to begin execution
Function Call (function with argument and a result) money = average(num_kids, funds);
Calls function average to compute a result that is stored in money
Function Definition (void function without arguments) void star_line(void) { printf(“*\n*\n*\n*\n”); }
Defines star_line as a function that prints a vertical line of four asterisks.
1-45
Construct
Effect
Function Definition (function with arguments and a result) /* *Returns the average of its 2 arguments *Pre : x and n are defined, x >= 0, n>0 *Post : result is x / n */ double average (int n, double x); { return (x/n) }
Defines average as a function that returns the result of dividing its second argument with its first argument
0