370 likes | 435 Views
KK10103 – Computer Programming. Chapter 3 : Top Down Design with Functions By Suraya Alias. Figure 3.2 Outline of Program Circle. 3.1 Building Programs from Existing Information. Case study Finding the Area and Circumference of a Circle 1) Problem Definition / Requirement
E N D
KK10103 – Computer Programming Chapter 3 : Top Down Design with Functions By Suraya Alias
3.1 Building Programs from Existing Information • 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
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
Figure 3.3 Calculating the Area and the Circumference of a Circle 5) Implementation 6) Testing
3.2 Library Function • Predefined Function and Code Reuse • By reusing predefined functions such as to perform mathematical function is called codereuse • 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(x) is the function call, • 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)
Figure 3.6 Function sqrt as a “Black Box” 1) Given y=sqrt(x); 2) x=16.0, so function squareroot computes the 3) The function result, 4.0 is assigned to y
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; • root_2=(-b-sqrt(disc))/2*a;
3.3 Top-Down Design and Structure Charts • 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
Figure 3.9 House and Stick Figure Case study : Drawing Simple Diagram
3.4 Function without Argument • 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
3.4 Function without Argument • Form : ftypefname(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
Figure 3.11 Function Prototypes and Main Function for Stick Figure
3.4 Function without Argument • Function Definitions • Syntax : ftype fname (void) { local declaration executable statements }
Figure 3.14 Program to Draw a Stick Figure.(Placement of Functions in a Program)
Figure 3.15 Flow of Control Between the main Function and a Function Subprogram
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.
Figure 3.16 Function instruct and the Output Produced by a Call
3.5 Function with Input Argument • 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
Figure 3.18 Function print_rboxed and Sample Run • 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)
Figure 3.22 Effect of Executing circum = find_circum (radius); If PI = 3.14159, radius=10.0circum = find_circum(radius) C subtitute the actual argument used in the function call for the formal parameter r
Figure 3.23 Function scale 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
Figure 3.24 Testing Function scale Actual Argument corresponds to Formal Parameter num_1 x num_2 n
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
Figure 3.25 Data Areas After Call scale(num_1, num_2); 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)