Introducing Modularity

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Chapter 6: User-Defined Functions I
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Chapter 6: Functions.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
1 -Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. How it works Programmer.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
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.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Computing Higher – SD Unit - Topic 8 – Procedure and Standard Algorithms P Lynch, St Andrew’s High School Unit 2 Software Development Process Topic.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Functions + Overloading + Scope
Programming Logic and Design Seventh Edition
Structured Programming
User-Written Functions
Programming Logic and Design Seventh Edition
Chapter 6: User-Defined Functions I
Functions Review.
Function There are two types of Function User Defined Function
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Functions CIS 40 – Introduction to Programming in Python
User-Defined Functions
Starting Out with Programming Logic & Design
Chapter 4 - Visual Basic Schneider
Chapter 6 Methods: A Deeper Look
Chapter 4 void Functions
Value returning Functions
MSIS 655 Advanced Business Applications Programming
Functions.
6 Chapter Functions.
Sub Procedures and Functions
Chapter 6: User-Defined Functions I
Starting Out with Programming Logic & Design
In C Programming Language
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Standard Version of Starting Out with C++, 4th Edition
CPS125.
C++ Object Oriented 1.
Presentation transcript:

Introducing Modularity

Goals By the end of this unit, you should understand … … why programmers use modularity. … what a sub-program does. … the difference between passing by value and passing by reference. … what a function does.

Defining Modularity Modularity is the process of taking larger programming tasks and breaking them into smaller, more manageable pieces. Sometimes, we call upon a module to perform a certain task without giving back a value to the procedure that called it. We call such modules sub-programs. When a module returns a value to its calling procedure, we call it a function. The main module is the central point in a program through which we call many of a program’s other modules.

Sharing Data Program modules do not have access to all of the variables from the other modules. However, sometimes we need to share data between modules. We share data by passing parameters from one module to another. Think of this as “exporting” data, and “importing” data. The following data flow diagram shows how modules share data …

Data Flow Between Modules

Parameters & Arguments Suppose we designed a module to output the results of some calculations. We will need to pass the data to that module so that it can do the output. Below is the syntax for defining a module that accepts data (has parameters) and how to call such a module passing the data (arguments). Notice that the names of the variables in the “calling” module do not need to be the same as the names in the “called” module. Call OutputResults(OrigPrice,DiscountRate,SalePrice) Sub OutputResults(OldPrice,Rate,NewPrice)

Why Use Arguments & Parameters? Modules become re-usable and fit more general purposes. Modules become easier to design and write. Programmers define modules in terms of what data they need from outside, what data they will produce for export. The programmer writing a module is not concerned with details outside of the module being developed. Module testing, or “unit testing” is an important mechanism to debug / maintain large programs.

How Should We Pass Values? When we transfer data between modules, we must also decide whether we should pass those values “by value” or “by reference.” This is a difficult topic to understand. Before going further, let’s take a step back and re-examine how memory stores variable values.

lvalue & rvalue lvalue rvalue 100000 34 100001 “Bob” 100010 true When a program tells memory to store data, memory creates a table as a directory of the stored data. In that table, memory stores two different values for each piece of data. It stores a location value, or lvalue. It also stores the actual value, called the read value, or rvalue. lvalue rvalue 100000 34 100001 “Bob” 100010 true 100011 47.89 100100 “peace”

Passing By Value Most of the of time, we want to protect our original values stored in variables. To prevent a called subprogram or function from manipulating those values, we send a copy of a variable’s rvalue to the parameter of the called procedure. We give this the name “passing arguments by value.” It is the default way to share data between modules in most programming languages.

Passing By Reference Sometimes, we want to give a called procedure the ability to manipulate a variable value. We can do this by passing the variable’s lvalue to the parameter of the called procedure. We give this the name “passing arguments by reference.” We should be very careful when sending by reference.

By Value/By Reference Example Main Program Set Num1 = 1 Set Num2 = 2 Call Switch (Num1, Num2) Write Num1, “ ”, Num2 End Program Subprogram Switch (Number1, Number2 As Ref) Set Number1 = 2 Set Number2 = 1 End Subprogram

Functions Functions are modules that return a value to a calling procedure. We classify functions into two groups: Built-In Functions are those that are inherent to a programming language. User-defined Functions are those modules that a programmer writes to return a value.

Writing a Function Since functions return a value, the definition of the function must include which data type of the return value. Sub Main() Declare Num as Float Set Num = Cube(10) Write Num End Main Function Cube(intX) as Float Set Cube = intX^3 End Cube

Questions?

Resources Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.