Starting Out with Programming Logic & Design

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
Advertisements

C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 2: Input, Processing, and Output
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 7: Methods.
Introduction to Methods
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Top-Down Design and Modular Development
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
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.
CPS120: Introduction to Computer Science Lecture 14 Functions.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Programming Logic and Design Seventh Edition
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 2: Input, Processing, and Output
by Tony Gaddis and Godfrey Muganda
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Function There are two types of Function User Defined Function
Chapter 5: Repetition Structures
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter Topics Chapter 5 discusses the following main topics:
Chapter 3 Simple Functions
Starting Out with Java: From Control Structures through Objects
Chapter 6 Methods: A Deeper Look
Chapter 4 void Functions
6 Chapter Functions.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Topics Introduction to Functions Defining and Calling a Function
A function is a group of statements that exist within a program for the purpose of performing a specific task. We can use functions to divide and conquer.
Starting Out with Programming Logic & Design
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 2: Input, Processing, and Output
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

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

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

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.

3.1 Introduction 5 benefits of using modules Simpler code Code reuse 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

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()

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.

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 Etc. End Module

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

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

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.

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

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.

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.

3.4 Passing Arguments to Modules Figure 3-14 Two arguments passed into two parameters

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.

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

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.