Download presentation
Presentation is loading. Please wait.
Published byTheodore Stewart Modified over 9 years ago
1
Chapter 3 : Top Down Design with Functions By Suraya Alias
2
1-2
3
1-3
4
Case study Finding the Area and Circumference of a Circle 1) Problem Definition / Requirement Get the radius of a circle. Compute and display the circle’s area and Circumference. 2) Analysis What are the data requirement and formula needed? Problem constant PI = 3.14159 Problem Input radius /*radius of a circle*/ Problem Output area /*area of a circle*/ Circum /*circumference of a circle*/ Relevant formula Area of a circle = π X radius² Circumference of a circle = 2 π X radius 1-4
5
3) Design Algorithm 1) Get the circle radius 2) Calculate the area 3) Calculate the circumference 4) Display the area and the circumference Step 2 needs refinement 2.1) Assign PI * radius * radius to area Step 3 needs refinement 3.1) Assign 2 * PI * radius to circum 1-5
6
1-6 5) Implementation 6) Testing
7
1-7
8
1-8
9
1-9
10
1- 10
11
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 defines a function named sqrt that perform the square root computation Example; y = sqrt(x), where sqrt(x) is the function call, sqrt is the function name, (x) is the argument Other functions are pow(x,y), log(x), exp(x), cos(x), tan(x), sin(x) 1- 11
12
1- 12 1) Given y=sqrt(x); 2) x=16.0, so function squareroot computes the 3) The function result, 4.0 is assigned to y
13
1- 13
14
1- 14
15
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; root_2=(-b-sqrt(disc))/2*a; 1- 15
16
1- 16
17
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
18
1- 18 Case study : Drawing Simple Diagram
19
1- 19
20
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
21
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
22
1- 22
23
Function Definitions Syntax : ftype fname (void) { local declaration executable statements } 1- 23
24
1- 24
25
1- 25
26
1- 26
27
1- 27
28
1- 28
29
Advantages of Using Function Subprograms 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
30
1- 30
31
Input Arguments Arguments used to pass information into a function subprogram 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
32
1- 32
33
1- 33 Void Functions with Input Arguments Actual Argument An expression used inside the parentheses of a function call, (135.68) Formal Parameter an identifier that represents a corresponding actual argument in a function definition (rnum)
34
1- 34
35
1- 35
36
1- 36
37
1- 37
38
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
39
1- 39 Program Style Function Interface comment Pre condition – a condition assumed to be true before a function call Post condition – a condition assumed to be true after a Function executes
40
1- 40 Actual Argument corresponds toFormal Parameter num_1x num_2n
41
1- 41
42
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
43
1- 43 The function call scale(2.5,-2) returns the value 0.025 where (2.5 X 10 ⁻ ²) From the formula (x * scale_factor) scale_factor = pow(10, n)
44
1- 44 ConstructEffect 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.
45
1- 45 ConstructEffect 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.