0% found this document useful (0 votes)
8 views

Programming Chap 3

Uploaded by

osesayjr
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Programming Chap 3

Uploaded by

osesayjr
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

MODULES

•Introduction to Modules
•Defining and Calling a Module
•Local Variables
•Passing Arguments to Modules
•Global Variables and Global Constants
Introduction to Modules

In Chapter 2, you saw a simple program that performs the


task of calculating an employee’s pay. Recall that the
program multiplied the number of hours that the employee
worked by the employee’s hourly pay rate. A more realistic
payroll program, however, would do much more than this.
In a real world application, the overall task of calculating
an employee’s pay would consist of several subtasks, such
as the following:
Introduction to Modules(Continue)

• Getting the employee’s hourly pay rate


• Getting the number of hours worked
• Calculating the employee’s gross pay
• Calculating overtime pay
• Calculating withholdings for taxes and benefits
• Calculating the net pay
• Printing the paycheck
Most programs perform tasks that are large enough to be
broken down into several subtasks. For this reason,
programmers usually break down their programs into
modules. A module is a group of statements that exist within
a program for the purpose of performing a specific task.
Introduction to Modules(Continue)
Instead of writing a large program as one long sequence of
statements, it can be written as several small modules, each one
performing a specific part of the task. These small modules can
then be executed in the desired order to perform the overall task.
For example, a realistic pay calculating program might have the
following modules:
•A module that gets the employee’s hourly pay rate
•A module that gets the number of hours worked
•A module that calculates the employee’s gross pay
•A module that calculates the overtime pay
•A module that calculates the withholdings for taxes and benefits
•A module that calculates the net pay
•A module that prints the paycheck
READ ON:
Benefits of using Modules
Defining and Calling a Module
Defining a Module
To create a module you write its definition. In most
languages, a module definition has two parts: a header and a
body. The header indicates the starting point of the module,
and the body is a list of statements that belong to the
module.

Here is the general format that we will follow when we write


a module definition in pseudocode:

Module showMessage() These statements are the body of


Display “Hello World.” the Module
Display “Hello World.”
End Module
Calling a Module
A module definition specifies what a module does, but it
does not cause the module to execute. To execute a
module, we must call it. In pseudocode we will use the
word Call to call a module. This is how we would call the
showMessage module:

Call showMessage()

When a module is called, the computer jumps to that


module and executes the statements in the module’s body.
Then, when the end of the module is reached, the
computer jumps back to the part of the program that
called the module, and the program resumes execution at
that point.
Example
Module main()
Display "I have a message for you."
Call showMessage()
Display "That's all, folks!"
End Module

Module showMessage()
Display "Hello world"
End Module
Flowcharting a Program with Modules
In a flowchart, a module call is shown with a rectangle that
has vertical bars at each side, as shown in below. The
name of the module that is being called is written on the
symbol.

showMessage()
Flowchart for Module Example 1

Main()
showMessage()

Display ”I have a
message for you.” Display “Hello
World”

showMessage()
Return

Display ”That’s all,


folks!”

End
Hierarchy Charts
Flowcharts are good tools for graphically depicting the flow
of logic inside a module, but they do not give a visual
representation of the relationships between modules.
Programmers commonly use hierarchy charts for this
purpose. A hierarchy chart, which is also known as a
structure chart, shows boxes that represent each module in
a program.
main()

calculateGross calculateOverti calculateWith calculateNetP


getInput()
Pay() me() holding() ay()

getHoursWorked() getHourlyPayRate() calculateTaxes() calculateBenefits()


Local Variables
A local variable is declared inside a module and cannot be
accessed by statements that are outside the module.
Different modules can have local variables with the same
names because the modules cannot see each other’s local
variables.
In most programming languages, a variable that is
declared inside a module is called a local variable. A local
variable belongs to the module in which it is declared, and
only statements inside that module can access the
variable. (The term local is meant to indicate that the
variable can be used only locally, within the module in
which it is declared.)
Local Variables (Example)
Module main()
Call getName()
Display "Hello ", name This will cause an error!
End Module

Module getName()
Declare String name This is local variable.
Display "Enter your name."
Input name
End Module
Scope and Local Variables

Module getName()
Display "Enter your name."
Input name This statement will cause an error
because
Declare String name the name variable has not been
declared yet.
End Module
Duplicate Variable Names
You cannot have two variables with the same name in the same
module because the compiler or interpreter would not know
which variable to use when a statement tries to access one of
them. All variables that exist within the same scope must have
unique names.
Module getTwoAges()
Declare Integer age
Display "Enter your age."
Input age

Declare Integer age This will cause an error!


Display "Enter your pet's age." A variable named age
has
Input age already been declared.
End Module
Duplicate Variable Names (Continue)
Note:
Although you cannot have two local variables with the
same name in the same module, it is usually okay for a
local variable in one module to have the same name as a
local variable in a different module. For example, suppose
a program has two modules: getPersonAge and getPetAge.
It would be legal for both modules to have a local variable
named age.
Passing Arguments to 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.
Example:
Module main()
Call doubleNumber(4) The argument 4 is copied into the value
End Module parameter variable

Module doubleNumber(Integer value)


Declare Integer result
Set result = value * 2
Display result
End Module
Passing Arguments to Module (COntinue)
Module main()
Declare Integer number
Display "Enter a number and I will display"
Display "that number doubled."
Input number
Call doubleNumber(number)
The contents of the number variable are
End Module copied into the value parameter variable

Module doubleNumber(Integer value)


Declare Integer result
Set result = value * 2
Display result
End Module
Passing Multiple Arguments to Module
Module main()
Display "The sum of 12 and 45 is:"
Call showSum(12, 45)
The contents of the number variable are
End Module copied into the value parameter variable

Module showSum(Integer num1, Integer num2)


Declare Integer result
Set result = num1 + num2
Display result
End Module
Passing Multiple Arguments to Module
Module main()
Display "The sum of 12 and 45 is:"
Call showSum(12, 45)
End Module The contents of the number variable are
copied into the value parameter variable

Module showSum(Integer num1, Integer num2)


Declare Integer result
Set result = num1 + num2
Display result
End Module
Global Variable and Global Constant

Global Variable

A global variable is a variable that is visible to every


module in the program. A global variable’s scope is the
entire program, so all of the modules in the program can
access a global variable.
In most programming languages, you create a global
variable by writing its declaration statement outside of all
the modules, usually at the top of the program.
Example shows how you can declare a global variable in
pseudocode.
Global Variable (Continue)
// The following declares a global Integer variable.
Declare Integer number
Module main()
Display "Enter a number."
Input number
Call showNumber()
End Module

Module showNumber()
Display "The number you entered is ", number
End Module
Global Variable Constant

A global constant is a named constant that is available to


every module in the program. Because a global constant’s
value cannot be changed during the program’s execution,
you do not have to worry about many of the potential
hazards that are associated with the use of global
variables.

Global constants are typically used to represent


unchanging values that are needed throughout a program.
For example, suppose a banking program uses a named
constant to represent an interest rate. If the interest rate
is used in several modules, it is easier to create a global
constant, rather than a local named constant in each
module. This also simplifies maintenance. If the interest
rate changes, only the declaration of the global constant
has to be changed, instead of several local declarations.
Example:

Marilyn works for Integrated Systems, Inc., a software


company that has a reputation for providing excellent
fringe benefits. One of its benefits is a quarterly bonus
that is paid to all employees. Another benefit is a
retirement plan for each employee. The company
contributes 5 percent of each employee’s gross pay and
bonuses to his or her retirement plan. Marilyn wants to
design a program that will calculate the company’s
contribution to an employee’s retirement account for a
year. She wants the program to show the amount of
contribution for the employee’s gross pay and for the
bonuses separately.
Solution:
Algorithm
1.Get the employee’s annual gross pay.
2.Get the amount of bonuses paid to the employee.
3.Calculate and display the contribution for the gross
pay.
4.Calculate and display the contribution for the bonuses.

Hierarchy Chart:
main()

getGrossPay(Re getBonuses(Real showGrossPayCont showBonuses(Real


al Ref grossPay) Ref bonuses) rib(Real grossPay) bonuses)
Pseudocode
// Global constant for the rate of contribution.
Constant Real CONTRIBUTION_RATE = 0.05
Module main() // main module
Declare Real annualGrossPay // Local variables
Declare Real totalBonuses // Local variables
Call getGrossPay(annualGrossPay) // Get the annual gross pay.
Call getBonuses(totalBonuses) // Get the total of the
bonuses.
// Display the contribution for the gross pay.
Call showGrossPayContrib(annualGrossPay)
// Display the contribution for the bonuses.
Call showBonusContrib(totalBonuses)
End Module
Module getGrossPay(Real Ref grossPay)
Display "Enter the total gross pay.“
Input grossPay
End Module

Module getBonuses(Real Ref bonuses)


Display "Enter the amount of bonuses."
Input bonuses
End Module
Module showGrossPayContrib(Real grossPay)
Declare Real contrib
Set contrib = grossPay * CONTRIBUTION_RATE
Display "The contribution for the gross pay"
Display "is $", contrib
End Module

Module showBonusContrib(Real bonuses)


Declare Real contrib
Set contrib = bonuses * CONTRIBUTION_RATE
Display "The contribution for the bonuses"
Display "is $", contrib
End Module
Global Global Constant
Constant Real
Real
contributionRate=0.05
contributionRate=0.05

You might also like