INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Communication between modules, cohesion and coupling
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Chapter 7: User-Defined Functions II
1 Software Design Introduction  The chapter will address the following questions:  How do you factor a program into manageable program modules that can.
Lesson 4 McManus COP  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries.
Programming Logic and Design Fourth Edition, Introductory
What is Software Design?  Introduction  Software design consists of two components, modular design and packaging.  Modular design is the decomposition.
Copyright Irwin/McGraw-Hill Software Design Prepared by Kevin C. Dittman for Systems Analysis & Design Methods 4ed by J. L. Whitten & L. D. Bentley.
Chapter 6: User-Defined Functions I
Program Design and Development
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Introduction to Programming Structure Lesson 4 McManusCOP10001.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 1 Program Design
Chapter 2: Algorithm Discovery and Design
Introduction to Methods
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
สาขาวิชาเทคโนโลยี สารสนเทศ คณะเทคโนโลยีสารสนเทศ และการสื่อสาร.
Introduction to Programming Structure Lesson 4 McManusCOP10061.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Programming Concepts Chapter 3.
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.
SE: CHAPTER 7 Writing The Program
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
Chapter 10 Software Engineering. Understand the software life cycle. Describe the development process models. Understand the concept of modularity in.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
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 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Introduction to OOP CPS235: Introduction.
Chapter 3: User-Defined Functions I
Systems Design.  Application Design  User Interface Design  Database Design.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
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.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
Computer Programming 12 Lesson 4 - Computer Programming Structure By Dan Lunney.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Programming Logic and Design Seventh Edition
Visit for more Learning Resources
Chapter 6: User-Defined Functions I
Coupling and Cohesion 1.
The Selection Structure
User-Defined Functions
User Defined Functions
Chapter 4 void Functions
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 6: User-Defined Functions I
Flow of Control.
Communication between modules, cohesion and coupling
Presentation transcript:

INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1

Overview 2  Pointers  Modules & Functions  Cohesion & Coupling  Local & Global Variables  Parameters  Variable Names & Data Dictionaries  Three Logic Structures

Pointers for Structuring a program 3  Use Modules  Each part should have a particular function  Use the four logic structures  Sequential, Decision, loop & case  Eliminate the rewriting of identical processes by using modules.  Use techniques to improve readability.

4 - The computer should be a tool to help people find solutions to their problems and to increase their productivity. - You can develop efficient computer solutions to problems if you heed the following pointers: 1- Use modules. Break the whole into parts, each part having a particular function. Pointers for Structuring a program

5 2- Use the four logic structures to ensure that solution flows smoothly from one instruction to the next, rather than jumping from one point in the solution to another. a- The sequential structure executes instructions one after another in a sequence. b- The decision structure branches to execute one of two possible sets of instructions. c- The loop structure executes a set of instructions many times. d- The case structure executes one set of instructions out of several sets. Pointers for Structuring a program

6 Sequential Logic Structure

7 Decision Logic Structure

8 Loop Logic Structure

9 Case Logic Structure

10

rules for designing modules  Each module  is an entity by itself. There is one entrance and one exit, the processing does not jump out of the middle of a module to another module.  has a single function ( printing, calculating, entering data)  should be easily read, modified ( short enough).  Length is governed by function and number of instructions contained within  Controls the order of processing. 11

Types of modules 12  Control modules.  Initialization module  Process module ( process only once or loop):  calculation modules.  print modules.  read and data validation modules.  Wrapup modules

Control Modules  Most often called “Main”  shows the overall flow of the data through the program  All other modules are subordinate to the control module. 13

Initialization Module  that are executed only once during the program and only at the beginning.  Examples Opening files Initializing variables ( beginning values ) 14

Process Modules  May be processed only once, or the may be part of a loop, there are several kind :  Calculation modules: arithmetic operations Accumulations Counting Manipulate string data.  Read and Data Validation modules: Reads and validates input data Usually separate modules  Print modules : print output line( the result of the processing) 15

Wrap Up Module  process all instructions that are executed once during the program and only at the end  Examples Closing files Printing totals. 16

Modules 17  Modules are often combined in one solution.  A module can be broken into several smaller modules to make the parts of a program more manageable.  One module performing a specific function can be used by one or more other modules.

COHESION & COUPLING chapter 4 18

Cohesion & Coupling 19  Each module should  Be functionally independent.  Perform single task.  Modules will need to connected,primarily through the data needed to complete the specified task within the modules ( cohesion and coupling ).

cohesion 20  The ability for a module to work independently from all other modules ( single entry and single exit).  Each module should have a function.  Each module should have single entry and single exit.

coupling 21  modules need to share data from other modules in order to complete the modular tasks.  is accomplished by some type of interface between modules that enables data to be passed from one module to another with the minimum interruption of the modular independence.  Allows for the communication between modules.  There are three ways to couple modules : global variable, parameters, and return value.

LOCAL AND GLOBAL VARIABLES  Programmer use the concept of local and global variables to allow cohesion and coupling to occur.  Global variable( define outside individual module )  Local variables ( define within module )  The difference between the two is in the scope of the variables.

Local variables 24  May be used only by the module itself.  Other modules have no knowledge of these variables.  Allow cohesion to take place.  The programmer does not have to worry about variable name duplication in modules created by other programmers.  The modules can be coupled through the use of parameters or return values.

global variables 25  thy can be seen by all modules.  Allow data coupling through the entire program.  The programmer must be careful not to use the same variable name for local variable and a global variable.  The use of parameter and return value is not necessary since global variable couple the data from module to module.

global variables  Can be referenced anywhere in the program  Is visible and accessible everywhere X, Y, Z A C B X, Y & Z are Global to modules A, B & C

Interactivity chart 27

Scope of Local and Global Variables 28

Parameters 29  Local variables that are passed from one module to another.  Another way of coupling that allow the communication of data between modules.  Thy are placed after module name between parentheses. Read ( A, B, C)

Parameters 30  Make modules more versatile.  Different data can be manipulated each time the module is called.  The calling module (module that process another module)  The called module ( the module being processed).

Parameter Terminology 31

Parameters 32  Parameters Comes in two types:  Actual (is the list of parameter that follows the module name being processed in the calling module )  Formal (is the list of parameter that follows the module name at the beginning of the module ).  The variable name in the actual may or may not be the same as those in the formal parameter.  The value are sent and received according to the position in the listings.

Actual & Formal Parameters 33  Actual Parameters  Parameters used in the call statement  Data types must be assignment compatible with its corresponding formal parameter  Can be a variable, constant or an expression  Can be call-by-value or call-by-reference  Formal Parameters  Must be a variable  Can be call-by-value or call-by-reference

Calling data through the modules  There are two ways to send data from one module to another:  Send the value of the variable ( call-by-value)  Send the address of the variable (call-by-reference) Specified by the use of an asterisk (*) in front of the variable name ( actual and formal parameter)

Call-by-Reference 35  the memory location is sent ( the address)  Is specified by the use of an asterisk( * ) in front of the variable name in both the actual and formal.  Gives access to the contents of the storage area where values are stored.  When the called module change the value of the parameter, the calling module will see the change.  They are using the same memory location.

Call-by-Value 36  the value of the variable is sent to the called module by the calling module.  Accomplished by creating a copy of the value  The called module will make a new memory location for this variable.  When the called module makes a change in the variable,the change will not be made in the calling module.  Parameter have different memory location

Parameters – Call-by-value and Call-by- Reference

Parameter Example

Coupling diagram  A coupling diagram indicates how data couples modules together  Has a rectangle for each module.  Line connecting the modules for each datum sent from one module to another.  The double-headed arrows indicate that these parameters are call - by - reference.  The single- headed arrows indicate that these parameters are call-by-value.

Coupling diagram

parameters  Parameter in the formal parameter listing are neither local nor global variables.  Should not be declared as local or global variables.  In the event of variable name duplication, the local variable has precedence over the parameter variable.  The parameter variable has precedence over the global variables.

Return value 45  Another way to couple modules.  The return value is the result of the function.  This is accomplished through the name of the function.  The result is placed temporarily in the name.  The function must be used as parts of another instruction. SquareRoot = 5 + SQRT ( 16 )  The value of SQRT is no longer available to be used

Variable Names and the data dictionary 46  Use a variable name that relates the name of the variable to its usage.  Variable name must be unique to the module in thy are defined.  Global variable must be unique to the total program.  The computer references values by their names.  Data dictionary is a great help to keep track of the variable usage in your program.And Defines all of the variables used within a program.

Example of a Data Dictionary

The four Logic Structures 48  control the logic of the data flow through the module.  The four basic logic structures :  Sequential logic structure  Decision logic structure.  Loop logic structure.  Case logic structure.