1 / 37

KK10103 – Computer Programming

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

luce
Télécharger la présentation

KK10103 – Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Figure 3.2 Outline of Program Circle

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

  4. 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

  5. Figure 3.3 Calculating the Area and the Circumference of a Circle 5) Implementation 6) Testing

  6. 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)

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

  8. Figure 3.7 Square Root Program

  9. Figure 3.7 Square Root Program (cont’d)

  10. 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;

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

  12. Figure 3.9 House and Stick Figure Case study : Drawing Simple Diagram

  13. Figure 3.10 Structure Chart for Drawing a Stick Figure

  14. 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

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

  16. Figure 3.11 Function Prototypes and Main Function for Stick Figure

  17. 3.4 Function without Argument • Function Definitions • Syntax : ftype fname (void) { local declaration executable statements }

  18. Figure 3.13 Function draw_triangle

  19. Figure 3.14 Program to Draw a Stick Figure.(Placement of Functions in a Program)

  20. Figure 3.14 Program to Draw a Stick Figure (cont’d)

  21. Figure 3.15 Flow of Control Between the main Function and a Function Subprogram

  22. 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.

  23. Figure 3.16 Function instruct and the Output Produced by a Call

  24. 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

  25. Figure 3.17 Lego® Blocks

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

  27. Figure 3.19 Effect of Executing print_rboxed(135.68);

  28. Figure 3.20 Function with Input Arguments and One Result

  29. Figure 3.21 Functions find_circum and find_area

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

  31. 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

  32. Figure 3.24 Testing Function scale Actual Argument corresponds to Formal Parameter num_1 x num_2 n

  33. Figure 3.24 Testing Function scale (cont’d)

  34. 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

  35. 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)

  36. New C Constructs

More Related