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.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

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.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
CS 201 Functions Debzani Deb.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Chapter 6: User-Defined Functions I
 2007 Pearson Education, Inc. All rights reserved C Functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Introduction to Methods
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
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
1 Introduction to Computers II Lecture 4 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
C++ Programming: Basic Elements of C++.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
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.
1 CS161 Introduction to Computer Science Topic #9.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Chapter 9 Functions Dept of Computer Engineering Khon Kaen University.
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.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
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.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
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.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
Programming Fundamentals Enumerations and Functions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Functions in C Mrs. Chitra M. Gaikwad.
User-Defined Functions
Lesson #6 Modular Programming and Functions.
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Top-Down Design with Functions
CPS125.
Top-Down Design with Functions
Presentation transcript:

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 analysis Design document (algorithm) –Previously written code Library functions

Building Programs from Existing Information 4 Problem analysis precedes requirements analysis 4 Requirements analysis precedes solution design 4 Solution design precedes programming 4 This sequence of steps (and successive steps) or phases is known as the software lifecycle

Building Programs from Existing Information 4 An algorithm can be developed following a methodology called stepwise refinement 4 In this methodology, an initial high-level solution is sketched, and each step in the algorithm is refined in a series of steps 4 We go from the human-oriented initial solution to the machine-oriented final program

Program Design Steps 4 Problem Statement 4 Problem Analysis 4 Data Requirements –Inputs –Outputs –Constants 4 Program Design –Initial Design Pseudocode

Program Design Steps –Algorithm refinement 4 Implementation 4 Testing –Test cases 4 Self-Check 3.1, 3.3

Library Functions 4 There are two major reasons to use functions –Code reuse –Divide and conquer approach to program design 4 C provides numerous functions to provide useful tasks –Similar functions are grouped into a library

Library Functions 4 We have already seen the standard i/o library –We can use the functions in this library by including the line “#include ” at the beginning of our program –The file is an example of a header file It gives the definitions of all of the functions and constants in the library so that they can be used by the compiler to check the program

Library Functions 4 Another important standard library (what is a standard library and how does it differ from a non-standard library?) is the standard math library - #include 4 Among the functions in this library are ceil(x) and floor(x) cos(x), sin(x) and tan(x) (all inputs given in radians) sqrt(x) and pow(x, y) exp(x), log(x) and log10(x) All have input, output of type double

Mathematical Functions 4 Consider the following use of the math functions –Given two sides (b and c) of a triangle and the angle between them (  the length of the third side can be computed using the following formula: a 2 = b 2 + c 2 -2bc cos  –In C, we compute a as follows: –a = sqrt(pow(b,2) + pow(c,2) -2 * b * c * cos(alpha * PI / 180.0));

User-Defined Functions 4 Besides the standard library functions (and main) C allows the programmer to define his own functions 4 The essential parts of the declaration of a function are: –Function name –Number of arguments and argument types –Return type

Top-Down Design and Structure Charts 4 As we said previously, top-down design is a methodology for program design –We apply the divide and conquer philosophy of a dividing a problem into subproblems –Concentrate on the subproblems one at a time –In the implementation, each subproblem may be implemented as a function –We may document a top-down design using a diagramming technique known as structure charts

Functions Without Arguments 4 The simplest functions are those which have no arguments and which return no value 4 These are also the most limited type of function since they always have the same outcome  An example of a function call for such a function is: do_input();

Functions Without Arguments 4 Just as we needed to declare variables prior to using them, we must also declare functions prior to use 4 The function is declared in a function prototype –The prototype gives the function’s name, argument types, and return types –If there is no value returned we use the reserved word void in place of the data value –If there are no arguments, use void

Functions Without Arguments  Example function prototype for a simple function: void do_input(void); 4 Function prototypes are placed after the includes and defines and before the main function of the program  Note that the header files for the standard libraries contain function prototypes for, e.g., scanf

Functions Without Arguments 4 After giving the function prototype, we must also have the function itself (function header and function body) 4 The function is placed after the function prototypes and before or after the main function (but not inside the main function!) 4 The function prototypes and preprocessor directives may also be place in a header file (when would you do this?)

Functions Without Arguments 4 If a function gets too big (say, > 200 lines of code) it should be split into multiple functions to make it easier to read 4 If a file gets too (say, > 1000 lines of code) it should be split into multiple files to make it easier to read 4 Functions should be commented to give their intended use

Flow of Control 4 A function call causes program execution to be transferred from the calling function to the called function  The return statement causes program execution to return to the calling function (or to the operating system in the case of the main function)

Functions with Arguments  Arguments carry information from the calling function (or operating system in the case of the main function) into the called function  Called functions can also return values to the calling function (or operating system in the case of the main function)

Functions with Arguments 4 Functions with arguments are much more powerful than those without since they can calculate a different value for each different argument 4 Arguments carry information into the function or allow multiple results to be returned by the function 4 Parameters can be classified as input or output arguments

Functions with Arguments  We can also have functions which have input parameters but a void return type –For example: print_sum(x,y); 4 Another possibility is to have a function with input arguments and a single result –In this case, the function call most often appears as part of an assignment statement –result = compute_sum(x,y);

Functions which return a value 4 These functions pass the value they compute back to the calling program using the return statement 4 A function to find the circumference of a circle might have a single statement - return: return(2.0*PI*r); /* r - argument */ 4 The arguments of a function are given names in the implementation of the function

Functions which return a value  double compute_sum(int first, int second) 4 We may then use the arguments of the function just as we would variables of the same types (only within the function though) 4 The values of the arguments are lost when we leave the function

Functions which return a value 4 Imagine we have a function called zero_out with an argument z: –int zero_out(int z); 4 Now in our main function we have an int variable x which we set to 3: –X = 3; 4 Call the function zero_out as follows: –y = zero_out(x);

Functions which return a value 4 In the body of zero_out we do the following: –z = 0; return(z); 4 What are the values of x and y? 4 If we have multiple arguments in the function, we must have a corresponding number of arguments in the function call and the must be of the same types as in the function prototype

Functions 4 If there are some variables that we need to perform the task of the function, we may declare these local variables at the start of the function body 4 Each time the function is called, the computer reserves space for the functions local variables and arguments (on the stack) 4 This space is freed when the function terminates

Testing Functions using Drivers 4 In top-down design, a programs functions may be tested separately from the programs which use them 4 In order to do this, write a short function which calls the function you want to test with some input and then displays the result 4 This calling function is called the driver function