Chapter 3 : Top Down Design with Functions By Suraya Alias.

Slides:



Advertisements
Similar presentations
Chapter 6: User-Defined Functions I
Advertisements

User Defined Functions
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Procedural programming in Java
Chapter 3 Top-Down Design with Functions. 3-2 Outline 3.1 BUILDING PROGRAMS FROM EXISING INFORMATION –CASE STUDY: FINDING THE AREA AND CIRCUMFERENCE OF.
Chapter 3 Top-Down Design with Functions Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
ICS103: Programming in C 3: Top-Down Design with Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
CS 201 Functions Debzani Deb.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
ICS103 Programming in C Lecture 9: Functions I
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
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.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
1 Introduction to Computers II Lecture 4 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 3 Slides By Dr. Daniyal Alghazzawi.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
1 ICS103 Programming in C Ch3: Top-Down Design with Functions.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Lecture 8: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
Chapter 3 Top-Down Design with Functions and Classes Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
chap3 Chapter 3 Top-Down Design with Functions.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Programming Fundamentals Enumerations and Functions.
1 ICS103 Programming in C Lecture 9: Functions I.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Functions Procedural Abstraction Flow of Control INFSY 307 Spring 2003 Lecture 4.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Chapter 9: Value-Returning Functions
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
Chapter 6: User-Defined Functions I
Function Topic 4.
User-Defined Functions
User Defined Functions
Lec8.
Chapter 6: User-Defined Functions I
CS149D Elements of Computer Science
Top-Down Design with Functions
CPS125.
Top-Down Design with Functions
Presentation transcript:

Chapter 3 : Top Down Design with Functions By Suraya Alias

1-2

1-3

 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 = 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

 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

1-6 5) Implementation 6) Testing

1-7

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

) Given y=sqrt(x); 2) x=16.0, so function squareroot computes the 3) The function result, 4.0 is assigned to y

1- 13

1- 14

 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

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 Case study : Drawing Simple Diagram

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

1- 22

 Function Definitions Syntax : ftype fname (void) { local declaration executable statements } 1- 23

1- 24

1- 25

1- 26

1- 27

1- 28

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

 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

1- 32

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)

1- 34

1- 35

1- 36

1- 37

1- 38 If PI = , radius=10.0 circum = find_circum(radius) C subtitute the actual argument used in the function call for the formal parameter r

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

1- 40 Actual Argument corresponds toFormal Parameter num_1x num_2n

1- 41

 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 The function call scale(2.5,-2) returns the value where (2.5 X 10 ⁻ ²) From the formula (x * scale_factor) scale_factor = pow(10, n)

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

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