Chapter 3: Modules. Starting Out with Programming Logic & Design. Second Edition. by Tony Gaddis

Similar documents
Introduction to Modules. Chapter 3. Defining and Calling a Module. Introduction to Modules. 5 Benefits of Modules. Modules CSUS, Spring 2016

Chapter 2: Input, Processing, and Output

Chapter 5: Methods. by Tony Gaddis. Starting Out with Java: From Control Structures through Objects. Fourth Edition

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

SNS COLLEGE OF ENGINEERING,

Chapter 1: Introduction to Computers and Java

8/23/2014. Chapter Topics. Introduction. Java History. Why Program? Java Applications and Applets. Chapter 1: Introduction to Computers and Java

Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Boca Raton Community High School AP Computer Science A - Syllabus 2009/10

Chapter 6 Methods. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Chapter 3: Decision Structures

Methods. CSE 114, Computer Science 1 Stony Brook University

by Pearson Education, Inc. All Rights Reserved. 2

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Java Software Solutions for AP Computer Science 3rd Edition, Lewis et al. 2011

Chapter 12. UML and Patterns. Copyright 2008 Pearson Addison-Wesley. All rights reserved

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical

Technology in Action. Alan Evans Kendall Martin Mary Anne Poatsy. Eleventh Edition. Copyright 2015 Pearson Education, Inc.

ECE 122. Engineering Problem Solving with Java

Method & Tools for Program Analysis & Design

Chapter 6: Methods. Objectives 9/21/18. Opening Problem. Problem. Problem. Solution. CS1: Java Programming Colorado State University

Chapter 11 Strings and Files. Starting Out with Games & Graphics in C++ Tony Gaddis

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Introduction to Flowcharting

Chapter 5 Functions. Dr. Zhang COSC 1436 Summer, 2018 June 19, 2018

AP Computer Science AB

FLOWCHARTS A flowchart is a graphical representation of the steps to be followed for solving problem. It consists of a set of symbols.

Chapter 4: Loops and Files

Technology in Action. Chapter Topics. Scope creep occurs when: 3/20/2013. Information Systems include all EXCEPT the following:

M.CS201 Programming language

Chapter 3: Decision Structures

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Functions. Introduction to Functions Introduction to Functions TOPICS C H A P T E R

Switching on our smartphone and sending an to a friend living 5000 km from our home is something that we take for granted, but that involves a

Unit II. (i) Computer Programming Languages

LECTURE 04 MAKING DECISIONS

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

Chapter Twelve. Systems Design and Development

Chapter 1 - An Introduction to Computers and Problem Solving

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

CHOICE BASED CREDIT SYSTEM (With effect from )

CHAPTER 1 Introduction to Computers and Java

Python - Week 1. Mohammad Shokoohi-Yekta

Structured Program Development in C

LECTURE 06 FUNCTIONS

Starting Out With Java Programming Challenges Solutions

Introduction to Computing Lecture 08: Functions (Part I)

Lesson 05 Methods. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

Lecture 5: Methods CS2301

Programming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs

1) What is the first step of the system development life cycle (SDLC)? A) Design B) Analysis C) Problem and Opportunity Identification D) Development

COURSE OUTLINES FACULTY OF ELECTRICAL ENGINEERING UNIVERSITI TEKNOLOGI MARA SHAH ALAM

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

About this exam review

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

6. C++ Subprograms David Keil CS I slides 7/03 1

4. The is a diagram that graphically depicts the steps that take place in a program. a. Program b. Flowchart c. Algorithm d. Code e.

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Welcome to Starting Out with Programming Logic and Design, Third Edition.

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, PILANI PILANI CAMPUS Instruction Division

Getting Deeper into. C++ Semantics. EECS 230 Winter 2018

Tutorial: Functions and Functional Abstraction. Nathaniel Osgood CMPT

Chapter 11: Inheritance

Introduction to Programming

ITE 205 Software Design and Programming I

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Lecture 04 FUNCTIONS AND ARRAYS

Interfaces CSC 123 Fall 2018 Howard Rosenthal

Technology in Action. Chapter Topics (cont.) Chapter Topics. Reasons for Software Programming. Information Systems 10/29/2010

Pearson Education 2005 Chapter 9 (Maciaszek - RASD 2/e) 2

C Programming. A quick introduction for embedded systems. Dr. Alun Moon UNN/CEIS. September 2008

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Numerical Methods in Scientific Computation

Smaller, simpler, subcomponent of program Provides abstraction

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CS110: PROGRAMMING LANGUAGE I

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Telling a Story Visually. Copyright 2012, Oracle. All rights reserved.

Top-down design. Topic 9. Functions II User-defined Functions. Component Testing. Flow of Top-Down Design

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Fundamentals of Python: First Programs. Chapter 6: Design with Functions

Chapter 3 Structured Program Development

Exercise: Inventing Language

Advance Excel Performing calculations on data 1. Naming groups of data 2. Creating formulas to calculate values

COURSE OUTLINES FACULTY OF ELECTRICAL ENGINEERING UNIVERSITI TEKNOLOGI MARA SHAH ALAM

Starting Out With C++: Standard Version By Tony Gaddis READ ONLINE

Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs

Textbook. Topic 5: Repetition. Types of Loops. Repetition

COP 1170 Introduction to Computer Programming using Visual Basic

AP Computer Science A Syllabus

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Transcription:

Chapter 3: Modules Starting Out with Programming Logic & Design Second Edition by Tony Gaddis Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter Topics 3.1 Introduction 3.2 Defining and Calling a Module 3.3 Local Variables 3.4 Passing Arguments to Modules 3.5 Global Variables and Global Constants Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2

3.1 Introduction A module is a group of statements that exists within a program for the purpose of performing a specific task. Most programs are large enough to be broken down into several subtasks. Divide and conquer: It s easier to tackle smaller tasks individually. Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-3

3.1 Introduction 5 benefits of using modules Simpler code Small modules easier to read than one large one Code reuse Can call modules many times Better testing Test separate and isolate then fix errors Faster development Reuse common tasks Easier facilitation of teamwork Share the workload Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-4

3.2 Defining and Calling a Module The code for a module is known as a module definition. Module showmessage() Display Hello world. End Module To execute the module, you write a statement that calls it. Call showmessage() Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-5

3.2 Defining and Calling a Module A module s name should be descriptive enough so that anyone reading the code can guess what the module does. No spaces in a module name. No punctuation. Cannot begin with a number. Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-6

3.2 Defining and Calling a Module Definition contains two parts A header The starting point of the module A body The statements within a module Module name( ) Statement Statement Etc. End Module Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-7

3.2 Defining and Calling a Module A call must be made to the module in order for the statements in the body to execute. Figure 3-2 The main module Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-8

3.2 Defining and Calling a Module When flowcharting a program with modules, each module is drawn separately. Figure 3-6 Flowchart for Program 3-1 Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-9

3.2 Defining and Calling a Module A top-down design is used to break down an algorithm into modules by the following steps: The overall task is broken down into a series of subtasks. Each of the subtasks is repeatedly examined to determine if it can be further broken down. Each subtask is coded. Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-10

3.2 Defining and Calling a Module A hierarchy chart gives a visual representation of the relationship between modules. The details of the program are excluded. Figure 3-7 A hierarchy chart Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-11

3.3 Local Variables A local variable is declared inside a module and cannot be accessed by statements that are outside the module. Scope describes the part of the program in which a variable can be accessed. Variables with the same scope must have different names. Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-12

3.4 Passing Arguments to Modules Sometimes, one or more pieces of data need to be sent to a module. An argument is any piece of data that is passed into a module when the module is called. A parameter is a variable that receives an argument that is passed into a module. The argument and the receiving parameter variable must be of the same data type. Multiple arguments can be passed sequentially into a parameter list. Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-13

3.4 Passing Arguments to Modules Figure 3-14 Two arguments passed into two parameters Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-14

3.4 Passing Arguments to Modules Pass by Value vs. Pass by Reference Pass by Value means that only a copy of the argument s value is passed into the module. One-directional communication: Calling module can only communicate with the called module. Pass by Reference means that the argument is passed into a reference variable. Two-way communication: Calling module can communicate with called module; and called module can modify the value of the argument. Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-15

3.5 Global Variables & Global Constants A global variable is accessible to all modules. Should be avoided because: They make debugging difficult Making the module dependent on global variables makes it hard to reuse module in other programs They make a program hard to understand Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-16

3.5 Global Variables & Global Constants A global constant is a named constant that is available to every module in the program. Since a program cannot modify the value of a constant, these are safer than global variables. Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-17