1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
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.
1 Lecture 3 Part 1 Functions with math and randomness.
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. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
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.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
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.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
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 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
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,
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
Function Topic 4.
FUNCTIONS IN C++.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
User-Defined Functions
Chapter 4: Subprograms Functions for Problem Solving
Dr. Shady Yehia Elmashad
User Defined Functions
Value returning Functions
CS150 Introduction to Computer Science 1
Chapter 6: User-Defined Functions I
Functions Imran Rashid CTO at ManiWeber Technologies.
Top-Down Design with Functions
Top-Down Design with Functions
Presentation transcript:

1 MODULAR DESIGN AND ABSTRACTION

2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.

3 STATE THE PROBLEM SUBDIVIDE THE PROBLEM INTO MAJOR SUBPROBLEMS SUBDIVIDE EACH SUBPROBLEM INTO SMALLER TASKS REPEAT THE PROCESS UNTIL TASKS ARE EASILY SOLVED. TOP- DOWN DESIGN

4 THE STRUCTURE CHART A GRAPHICAL REPRESENTATION OF SYSTEM COMPONENTS. USED TO IDENTIFY AND DEFINE RELATIONSHIPS AMONG SUBPROBLEMS.

5 main module2 module1module3 A STRUCTURE CHART LEVEL O LEVEL 1

6 EACH SUBDIVISION IS REFERRED TO AS A STEPWISE REFINEMENT. TASKS AT EACH STAGE OF THE SUBDIVISION ARE REFERRED TO AS MODULES. STEPWISE REFINEMENT

7 PROBLEM SOLVING WITH TOP- DOWN DESIGN PROBLEM: GET THREE INTEGER NUMBERS REPRESENTING TEST MARKS. COMPUTE AND DISPLAY THEIR AVERAGE.

8 FindAvg Calculate Average Get Numbers Display Results THE STRUCTURE CHART FOR PROGRAM FindAvg: LEVEL 0 LEVEL 1

9 THE ALGORITHM: 1 GET test1, test2, test3 2 CALCULATE average 3 DISPLAY average

10 NEW CONCEPTS MODULES

11 MODULAR DESIGN AND FUNCTIONS MODULES ARE CODED AS SUBPROGRAMS. A C++ FUNCTION IS A GROUPING OF STATEMENTS INTO A SINGLE UNIT THAT PERFORMS A TASK. EACH C++ PROGRAM IS MADE UP OF ONE OR MORE FUNCTIONS ( main() IS A FUNCTION).

12 FUNCTION PROTOTYPE A FUNCTION MUST BE DECLARED BEFORE IT MAY BE CALLED. type function_name (); // FINDS THE AVERAGE OF THREE NUMBERS void Find_average ();

13 FUNCTION CALLS A FUNCTION IS CALLED THROUGH THE EXECUTION OF A FUNCTION CALL. LOGIC FLOW IS TRANSFERRED TO function_name. // A CALL TO FUNCTION Find_average Find_average (); function_name ();

14 FUNCTION DEFINITION A FUNCTION CONSISTS OF 2 PARTS: THE HEADING AND THE BODY. THE HEADING INCLUDES A TYPE SPECIFICATION, FUNCTION NAME, AND LEFT AND RIGHT PARENTHESES. THE BODY CONSISTS OF LOCAL DECLARATIONS AND EXECUTABLE STATEMENTS. THE FUNCTION BODY BEGINS WITH A LEFT BRACE AND ENDS WITH A RIGHT BRACE.

15 type function_name ( ) { local_declarations executable_statements } heading body

16 void Find_average() { // Calculates the average const int NUMBER_OF_TESTS = 3; average = (test1 + test2 + test3) / NUMBER_OF_TESTS; return; } // end Find_average EXAMPLE:

17 FLOW OF CONTROL FUNCTIONS ARE DEFINED PHYSICALLY AFTER THE MAIN PROGRAM. EXECUTION STARTS WITH THE STATEMENTS OF THE MAIN PROGRAM. THE CALL STATEMENT ACTS AS TRANSFER OF CONTROL. THE RETURN STATEMENT SENDS THE CONTROL BACK TO THE CALLING MODULE.

18 EXAMPLE:... void main () {... Get_data (); Find_average (); Display_results (); return; } void Find_average () { // Calculates the average const int NUMBER_OF_TESTS = 3; average = ( test1 + test2 + test3 ) / NUMBER_OF_TESTS; return; } // end Find_average ()

19 // FILE: FindAvg2.cpp // Finds the average of three test scores #include using std::cout; using std::cin; using std::endl; // global data: float test1, test2, test3, average; voidmain () { // functions used: void Get_data (); void Find_average (); void Display_results (); // function calls: Get_data (); Find_average (); Display_results (); return; } THE CODE FOR FindAvg:

20 void Get_data () { // Gets the grade marks cout << "Enter the three test marks: "; cin >> test1 >> test2 >> test3; return; } void Find_average() { // Calculates the average const int NUMBER_OF_TESTS = 3; average = (test1 + test2 + test3) / NUMBER_OF_TESTS; return; } void Display_results () { // Displays the average cout << "The average of the 3 test marks is " << average << endl; return; }

21 LOCAL DECLARATIONS OBJECTS DECLARED LOCALLY WITHIN A FUNCTION CAN BE REFERENCED ONLY WITHIN THAT FUNCTION.

22 GLOBAL DECLARATIONS OBJECTS DECLARED OUTSIDE OF FUNCTIONS CAN BE REFERENCED FROM WITHIN ANY FUNCTION THAT FOLLOWS THAT DECLARATION.

23 PARAMETERS ARE USED TO COMMUNICATE BETWEEN MODULES. THE CALLING MODULE PASSES VALUES TO THE CALLED MODULE TO USE THEM IN PROCESSING. THE CALLED MODULE RETURNS THE RESULTS TO THE CALLING MODULE. MODULE COMMUNICATION

24 EXAMPLE: PROBLEM GIVEN THE RADIUS OF A CIRCLE, COMPUTE AND DISPLAY THE AREA AND THE CIRCUMFERENCE.

25 ANALYSIS AND SPECIFICATIONS IN ORDER TO COMPUTE THE CIRCLE’S AREA AND CIRCUMFERENCE (PROBLEM’S OUTPUT), WE NEED THE RADIUS (PROBLEM’S INPUT). WE ALSO KNOW THAT THE AREA MAY BE CALCULATED AS:  x RADIUS 2 AND THE CIRCUMFERENCE MAY BE CALCULATED AS: 2 x π  x RADIUS. DATA REQUIREMENTS CONSTANT π = INPUT RADIUS (FLOAT) OUTPUT AREA (FLOAT) CIRCUMFERENCE (FLOAT) FORMULAE AREA = π * RADIUS * RADIUS CIRCUMFERENCE = 2 * π * RADIUS

26 DESIGN HAVING DEFINED THE PROBLEM’S INPUT AND OUTPUT, WE MAY START IDENTIFYING THE SYSTEM’S COMPONENTS AND STEPS NECESSARY TO SOLVE THE PROBLEM.

27 AreaCircm Compute Get radiusDisplay Result LEVEL 0 LEVEL 1 STRUCTURE CHART

28 AreaCircm Compute Get radiusDisplay Result LEVEL 0 LEVEL 1 areacircum LEVEL 2 STRUCTURE CHART

29 1. GET DATA 2. COMPUTE 3. DISPLAY RESULTS ALGORITHM

30 1. GET DATA 2. COMPUTE 2.1 COMPUTE AREA 2.2 COMPUTE CIRCUMFERENCE 3. DISPLAY RESULTS ALGORITHM: A Second Level Refinement

31 NEW CONCEPTS MODULES COMMUNICATION

32 FUNCTION PROTOTYPE USING PARAMETERS type function_name (parameter_type_list); // computes the area of a circle float Compute_area (float r);

33 FUNCTION CALLS function_name (parameter_list); // A call to function Compute_area() Compute_area (radius);

34 FUNCTION DEFINITION USING PARAMETERS type function_name ( parameter_list) { local_declarations executable statements } heading body

35 EXAMPLE: // computes the area of a circle float Compute_area (float r) { // compute and return the area return PI * r * r; } // end Compute_area () DEFINITION:

36 CALL: area = Compute_area (radius);

37 SAME RULES AS WITH IDENTIFIERS. void Module_name (float first, float second, float third, float result); OR void Calculate_average (float test1, float test2, float test3, float average); THE SECOND MODULE NAME IS MORE MEANINGFUL. NAMING RULES

38 // FILE: AreaCircm.cpp // FINDS AND PRINTS THE AREA AND CIRCUMFERENCE OF A CIRCLE #include #include "CmptArCr.cpp" //Compute_area and Compute_circum using std::cout; using std::cin; using std::endl; void main () { // functions used: // COMPUTES THE AREA OF A CIRCLE float Compute_area (float); // COMPUTES THE CIRCUMFERENCE OF A CIRCLE float Compute_circum (float); IMPLEMENTATION FOR AreaCircm

39 // local data: float radius; // radius of circle float area; // area of circle float circum; // circumference of circle // get radius; cout << "enter the circle radius: "; cin >> radius; // compute the area of the circle area = Compute_area (radius); // compute the circumference of the circle circum = Compute_circum (radius); cout << "the area of the circle is " << area << endl; cout << "the circumference of the circle is " << circum << endl; return; }

40 // FILE: CmptArCr.cpp // Modules to calculate the area and // circumference of a circle. // global data: const float PI = ; // COMPUTES THE AREA OF A CIRCLE float Compute_area (float r) { // compute and return the area return PI * r * r; } // end Compute_area ()

41 // COMPUTES THE CIRCUMFERENCE OF A CIRCLE float Compute_circum (float r) { // compute and return the circumference return 2.0 * PI * r; } // end Compute_circum ()

42 THE PARAMETERS IN THE CALL STATEMENT ARE REFERRED TO AS ACTUAL PARAMETERS. THE VALUE OF AN ACTUAL PARAMETER IS PASSED AND USED BY THE FUNCTION. ACTUAL PARAMETERS

43 FORMAL PARAMETERS THE PARAMETERS IN THE FUNCTION HEADING ARE REFERRED TO AS FORMAL PARAMETERS. A FORMAL PARAMETER REPRESENTS THE ACTUAL VALUE PASSED TO THE FUNCTION EACH TIME IT IS CALLED.

44 LIBRARIES OF FUNCTIONS PROVIDE ACCESS TO PREVIOUSLY WRITTEN FUNCTIONS THAT PERFORM USEFUL DIAGNOSTICS, MANIPULATIONS, COMPUTATIONS, ETC.

45 EXAMPLES OF SOME MATHEMATICAL FUNCTIONS result = sqrt (16) * 10 RETURNS THE VALUE THE SQUARE ROOT OF 16 IS CALCULATED, THEN THE VALUE IS MULTIPLIED BY 10. floor (3.8) = 3 RETURNS THE LARGEST INTEGER NOT GREATER THAN THE PARAMETER. fabs (-3.8) = 3.8 RETURNS THE ABSOLUTE VALUE.