CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions • C Library functions • Case studies • Top-down design and structure charts • Basic concepts about functions – Prototype, definition and function call – Input arguments – Output arguments – Void function and void argument – Actual parameter / formal parameter – Local variable • Building programs from existing information
19
Embed
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1
Top-Down Design with Functions
• C Library functions• Case studies • Top-down design and structure charts
• Basic concepts about functions– Prototype, definition and function call– Input arguments– Output arguments– Void function and void argument– Actual parameter / formal parameter– Local variable
• Building programs from existing information
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 2
Reuse of Existing Code
• C has a rich function library consists many predefined functions.– What are they? arguments, output, and their data types.
• Use C library functions– To simply a program– To reduce the errors– To write program efficiently
• Example: mathematical function library – contains commonly used math function routines: ceil(x), cos(x),
printf("Enter the coefficients a, b, and c:\n"); scanf("%lf%lf%lf", &a, &b, &c);
disc = b*b - 4*a*c; sqrt_disc = sqrt(disc); /* sqrt() is a math function in math lib */ root_1 = (-b + sqrt_disc)/(2*a); root_2 = (-b - sqrt_disc)/(2*a);
• Problem constants: PI 3.14159• Area = PI * radius 2
• Circum = 2* PI * radius– Data requirements: double radius, area, circum
• Algorithm design1. Get the radius of a circle2. Calculate the area: area = PI * radius*radius3. Calculate the circumference: circum = 2*PI*radius4. Display the area and the circumference
• Implementation
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 7
Calculating the Area and the Circumference of a Circle
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 8
User Defined Function and Top-Down Design Method
• C allow a user to define a function. This makes th top-down design possible
• Top-down design_ a problem-solving method in which one first break a problem up into its major subproblems and then solve the subproblems to derive the solution to the original problem
• Structure chart __ a documentation tool that shows the relationships among the sub-problems of a problem.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 9