Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.

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

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
© 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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Introduction to Methods
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Top-Down Design and Modular Development
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
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 © 2012 Pearson Education, Inc. Chapter 6: Functions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
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.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 2: Input, Processing, and Output
Function There are two types of Function User Defined Function
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Starting Out with Programming Logic & Design
Chapter 3 Simple Functions
Chapter 6 Methods: A Deeper Look
Chapter 4 void Functions
Topics Introduction to Functions Defining and Calling a Void Function
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
Chapter 2: Input, Processing, and Output
Presentation transcript:

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis Chapter 7: Modules

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-2 Chapter Topics 7.1 Introduction 7.2 Defining and Calling a Module 7.3 Local Variables 7.4 Passing Arguments to Modules 7.5 Global Variables and Global Constants

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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

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

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

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-10 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. 7.2 Defining and Calling a Module

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

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-14 Figure 7-14 Two arguments passed into two parameters 7.4 Passing Arguments to Modules

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-15 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. 7.4 Passing Arguments to Modules

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-17 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. 7.5 Global Variables & Global Constants