Starting Out with Programming Logic and Design PDF
Document Details
Uploaded by HappierNurture8809
Universiti Kuala Lumpur
2022
Tony Gaddis
Tags
Summary
This book, Starting Out with Programming Logic and Design, Sixth Edition, provides a comprehensive introduction to programming logic and design concepts. It focuses on the key aspects of modularizing code using programming concepts such as modules, local variables, and arguments. Examples in Java, Python and C++ support these concepts.
Full Transcript
Starting out with Programming Logic and Design Sixth Edition Chapter 5 Modules Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved Le...
Starting out with Programming Logic and Design Sixth Edition Chapter 5 Modules Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved Learning Objectives 5.1 Introduction 5.2 Defining and Calling a Module 5.3 Local Variables 5.4 Passing Arguments to Modules 5.5 Global Variables and Global Constants 5.6 Focus on Languages: Java, Python, and C++ Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.1 Introduction (1 of 2) A module is a group of statements that exists for the purpose of performing a specific task within a program. Most programs are large enough to be broken down into several subtasks. Divide and conquer: It’s easier to tackle smaller tasks individually. Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.1 Introduction (2 of 2) 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 – Easier Maintenance ▪ Smaller, simpler code is easier to maintain Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.2 Defining and Calling a Module (1 of 7) 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.2 Defining and Calling a Module (2 of 7) 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.2 Defining and Calling a Module (3 of 7) 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.2 Defining and Calling a Module (4 of 7) A call must be made to the module in order for the statements in the body to execute. Figure 5-2 The main module Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.2 Defining and Calling a Module (5 of 7) When flowcharting a program with modules, each module is drawn separately. Figure 5-6 Flowchart for Program 5-1 Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.2 Defining and Calling a Module (6 of 7) 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.2 Defining and Calling a Module (7 of 7) A hierarchy chart gives a visual representation of the relationship between modules. The details of the program are excluded. Figure 5-9 A hierarchy chart Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.3 Local Variables (1 of 3) A local variable is declared inside a module and cannot be accessed by statements that are outside the module. A variable’s scope is the part of the program in which the variable can be accessed. Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.3 Local Variables (2 of 3) Duplicate Variable Names: Variables within the same scope must have different names. Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.3 Local Variables (3 of 3) Duplicate Variable Names: Variables in different scopes can have the same name. Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.4 Passing Arguments to Modules (1 of 3) 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.4 Passing Arguments to Modules (2 of 3) Figure 5-17 Two arguments passed into two parameters Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.4 Passing Arguments to Modules (3 of 3) 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.5 Global Variables & Global Constants (1 of 2) 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.5 Global Variables & Global Constants (2 of 2) 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 © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Java Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Java (1 of 9) In Java, modules are called methods. To create a method in Java, you must write its definition, which consists of : – A header that specifies the method’s name – A method body which is a collection of statements that are performed when the method is executed public static void showMessage() Header { System.out.println("Hello world"); Body } Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Java (2 of 9) When a method is called, the program branches to that method and executes the statements in its body. showMessage(); Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Java (3 of 9) Local Variables – Variables that are declared inside a method are local variables. – Statements outside a method cannot access that method’s local variables. public static void myMethod() { int value = 5; System.out.println(value); } Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Java (4 of 9) Passing Arguments to Methods – To pass an argument into a method, you must declare a parameter variable in that method's header – The parameter variable will receive the argument that is passed when the method is called public static void displayValue(int num) { System.out.println("The value is " + num); } Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Java (5 of 9) Passing Arguments to Methods – To pass an argument into a method, you must declare a parameter variable in that method's header – The parameter variable will receive the argument that is passed when the method is called public static void displayValue(int num) { System.out.println("The value is " + num); } Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Java (6 of 9) Passing Arguments to Methods displayValue(5); public static void displayValue(int num) { System.out.println("The value is " + num); } Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Java (7 of 9) Passing Multiple Arguments showSum(10, 20); public static void showSum(int num1, int num2) { int result; result = num1 + num2; System.out.println(result); } Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Java (8 of 9) Arguments are Passed By Value – In Java, only a copy of an argument’s value is passed into a parameter variable. – If a parameter variable is changed inside a method, it has no effect on the original argument. Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Java (9 of 9) public class FieldTest Global Constants { – In Java, variables and constants public static final int VALUE = 10; cannot be declared outside of a public static void main(String[] args) class { // Statements here have access to – If you declare a variable or // the VALUE constant. constant inside a class, but outside } of all the class's methods, that public static void method2() variable or constant is known as a { class field, and will be available to // Statements here have access to // the VALUE constant. all the methods within the class. } public static void method3() { // Statements here have access to // the VALUE constant. } } Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Python Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Python (1 of 10) In Python we use functions to modularize code To create a function in Python, you must write its definition, which consists of : – A header that specifies the function's name – A function body which is a collection of statements that are performed when the function is executed def message(): Header print('I am Arthur,') Body print('King of the Britons.') Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Python (2 of 10) When a function is called, the program branches to that function and executes the statements in its body. def message(): print('I am Arthur,') print('King of the Britons.') message() Program Output I am Arthur, King of the Britons. Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Python (3 of 10) Indentation: – In Python, each line in a block must be indented. Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Python (4 of 10) Local Variables – Variables that are declared inside a function are local variables. – Statements outside a function cannot access that function's local variables. def myFunction(): value = 5 print(value) Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Python (5 of 10) Passing Arguments to Functions – To pass an argument into a function, you must declare a parameter variable in that function's header – The parameter variable will receive the argument that is passed when the function is called def double_number(value): result = value * 2 print(result) Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Python (6 of 10) Passing Arguments to Functions double_number(5) def double_number(value): result = value * 2 print(result) Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Python (7 of 10) Passing Multiple Arguments show_sum(10, 20); show_sum(num1, num2): result = num1 + num2 print(result) Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Python (8 of 10) Arguments are Passed By Value – In Python, only a copy of an argument’s value is passed into a parameter variable. – If a parameter variable is changed inside a function, it has no effect on the original argument. Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Python (9 of 10) Global Variables # Create a global variable. – In Python, when a variable is my_value = 10 created by an assignment statement that is written outside all # The show_value function prints the functions in a program file, the # the value of the global variable. variable is global def show_value(): print(my_value) – A global variable can be accessed by any statement in the program # Call the show_value function. file, including the statements in any show_value() function Program Output 10 Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: Python (8 of 10) Global Constants – Python does not allow you to create true global constants – However, you can simulate them with global variables – If you do not declare a global variable with the global key word inside a function, then you cannot change the variable's assignment. Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: C++ Copyright © 2022, 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved 5.6 Focus on Languages: C++ (1 of 8) In C++ we use functions to modularize code To create a function in C++, you must write its definition, which consists of : – A header that specifies the function's name – A function body which is a collection of statements that are performed when the function is executed void showMessage() Header { cout