Professional Documents
Culture Documents
Lecture No. 3
Lecture No. 3
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)
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)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.2 Defining and Calling a Module (2 of 7)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.2 Defining and Calling a Module (3 of 7)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.2 Defining and Calling a Module (4 of 7)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.2 Defining and Calling a Module (5 of 7)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.2 Defining and Calling a Module (6 of 7)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.2 Defining and Calling a Module (7 of 7)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.3 Local Variables (1 of 3)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.3 Local Variables (2 of 3)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.3 Local Variables (3 of 3)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.4 Passing Arguments to Modules (1 of 3)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.4 Passing Arguments to Modules (3 of 3)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.5 Global Variables & Global
Constants (1 of 2)
Copyright © 2016, 2013, 2010 Pearson Education, Inc. All Rights Reserved
3.5 Global Variables & Global
Constants (2 of 2)
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
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.
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
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
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);
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);
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;
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
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
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
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);
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);
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.
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