You are on page 1of 50

Starting out with Programming Logic and

Design
Fifth Edition

Chapter 3
Modules

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Learning Objectives

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.6 Focus on Languages: Java, Python, and C++

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.2 Defining and Calling a Module (1 of 7)

• The code for a module is known as a module definition.

• To execute the module, you write a statement that calls it.

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 3-2 The main module

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.2 Defining and Calling a Module (5 of 7)

• When flowcharting a program with modules, each module


is drawn separately.

Figure 3-6 Flowchart for


Program 3-1

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 3-7 A hierarchy chart

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.3 Local Variables (2 of 3)

• Duplicate Variable Names: Variables within the same


scope must have different names.

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.3 Local Variables (3 of 3)

• Duplicate Variable Names: Variables in different scopes


can have the same name.

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.4 Passing Arguments to Modules (2 of 3)
Figure 3-15 Two arguments passed into two parameters

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.6 Focus on Languages: Java

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.6 Focus on Languages: Python

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.6 Focus on Languages: Python (3 of 10)
• Indentation:
– In Python, each line in a block must be indented.

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.6 Focus on Languages: C++

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.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 << "Hello world" << endl; Body
}

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.6 Focus on Languages: C++ (2 of 8)
• When a function is called, the #include <iostream>
using namespace std;
program branches to that
function and executes the void showMessage(); Function prototype
statements in its body. (Declares the function's
int main() existence)
{
cout << "I have a message for you." << endl;
showMessage(); Function call
cout << "That's all, folks!" << endl;
return 0;
}

void showMessage()
{
cout << "Hello world" << endl;
}

Program Output
I have a message for you.
Hello world
That's all, folks!
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.6 Focus on Languages: C++ (3 of 8)
• Local Variables
– Variables that are declared inside a function are local variables.
– Statements outside a function cannot access that function's local variables.

void myFunction()
{
int value = 5;
cout << value << endl;
}

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.6 Focus on Languages: C++ (4 of 8)
• 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

void displayValue(int num)


{
cout << "The value is " << num << endl;
}

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.6 Focus on Languages: C++ (5 of 8)
• Passing Arguments to Functions

doubleNumber(5);

void doubleNumber(int value)


{
int result = value * 2;
cout << result << endl;
}

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.6 Focus on Languages: C++ (6 of 8)
• Passing Multiple Arguments

showSum(10, 20);

void showSum(int num1, int num2)


{
int result = num1 + num2;
cout << result << endl;
}

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.6 Focus on Languages: C++ (7 of 8)
• C++ provides a special type of variable called a reference variable that, when used as
a function parameter, allows access to the original argument.

• Any changes made to the reference parameter variable are performed on the actual
argument that was passed into the parameter.

• To declare a parameter variable as a reference variable, place an ampersand (&) in


front of the name.

void setToZero(int &num)


{
num = 0;
}

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.6 Focus on Languages: C++ (8 of 8)
#include <iostream>
using namespace std;
• Global Constants
// Function prototypes
– If a variable or constant declaration void function2();
is written outside of all functions, void function3();
and above the definitions of the // Global constant
functions, it becomes a global const double INTEREST_RATE = 0.05;
variable.
int main()
– All the functions in the program will {
// Statements here have access to
have access to the variable or // the INTEREST_RATE constant.
constant. return 0;
}

void function2()
{
// Statements here have access to
// the INTEREST_RATE constant.
}

void function3()
{
// Statements here have access to
// the INTEREST_RATE constant.
}

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
Copyright

Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved

You might also like