2. What we’ll Learn
• Defining and calling a module
• Passing arguments to module
• Hierarchy chart
• Pseudocode
• Flow chart
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
2
4. A module
is a
group of statements that exist within a
program for the purpose of performing
a specific task
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
4
6. How to Define Module
Module name()
statement
statement
etc
End Module
A header (starting point)
A body
End point
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
6
7. Example
Module main()
Display "I have a message for you."
Call showMessage()
Display "That's all, folks!"
End Module
To execute a module,
you call it
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
7
8. Another 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
The program begins executing at
the main module.
When the end of the main module
is reached, the program stops
executing.
The computer jumps to
the showMessage module and executes
the statements in its body.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
8
9. Local vs Global Variables
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
9
10. Local Variable
Declared inside a module
Cannot be accessed by statements that are outside the module
Different modules can have local variables with the same
names
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
10
11. Example Local Variable
Module main()
Call getName()
Display "Hello"
End Module
Module getName()
Declare String name
Display "Enter your name"
Input name
End Module
Local variable
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
11
12. Global Variables
Is accessible to all the module
Declaration statement is outside of all module
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
12
13. Example Global 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
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
13
14. Passing Arguments to Modules
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
14
15. • An argument is any piece of data that is passed into a module when
the module is called
• If you want a module to receive arguments when it is
called, you must equip the module with one or more
parameter variables.
• A parameter is a variable that receives an argument that is
passed into a module.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
15
17. Two Type of Passing Arguments
Value
• Only a copy of the argument’s
value is passed into the
parameter variable
• If the contents of the parameter
variable are changed inside the
module, it has no effect on the
argument in the calling part of
the program
Reference
• The argument is passed into a
special type of parameter known
as a reference variable.
• When a reference variable is
used as a parameter in a module,
it allows the module to modify
the argument in the calling part
of the program
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
17
19. Example Passing Arguments by References
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
19
20. Passing multiple arguments
• Most languages allow you to write modules that accept multiple
arguments.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
20
22. Argument and the receiving parameter variable be
of the same data type
If you try to pass different type, an error usually
occurs
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
22
32. Terminal / Terminator
• Marks the starting or
ending point of the
system
• Usually contains the
word “Start” or “End”
Action/Process
• Represent a single step
or and entire sub-
process within a larger
process
Document
• Printed document or
report
Data/Input/Output
• Information entering as
input
• Information display as
output
Decision
• System decision
Database
• Storage information
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
32
34. Summary
• Programmers break down programming problems into reasonable units called modules,
subroutines, procedures, functions, or methods.
• To execute a module, you call it from another program or module
• When you create a module, you include a header, a body, and a return statement.
• When a main program wants to use a module, it “calls” the module’s name
• Th e variables and constants declared in a module are usable only within the module; they
are local to the module. Global variables and constants are those that are known to the
entire program.
• You can use a hierarchy chart to illustrate modules’ relationships. A hierarchy chart tells
you which modules exist within a program and which modules call others.
18/9/2018
CSWD1001 @ Kwan Lee First City Unversity Malaysia
(FCUC)
34
Reason - Programmers seldom write programs as one long series of steps.
#include <iostream>
using namespace std;
//Declare module
int number1, number2;
int showSum(int value1, int value2);
int main(){
cout << "Enter first number: ";
cin >> number1;
cout << "Enter second number: ";
cin >> number2;
showSum(number1, number2);
}
//Module changeMe
int showSum(int value1, int value2){
cout << "Number 1: " << number1 << endl;
cout << "number 2: " << number2 << endl;
int sum = number1 + number2;
cout << "Total is: " << sum;;
}
When you pass an argument to a module, most programming languages require that the argument and the receiving parameter variable be of the same data type.
NOTE: Some languages allow you to pass an argument into a parameter variable
of a different type as long as no data will be lost. For example, some languages allow
you to pass integer arguments into real parameters because real variables can hold
whole numbers. If you pass a real argument, such as 24.7, into an integer parameter,
the fractional part of the number would be lost.
#include <iostream>
using namespace std;
//Declare module without return value
void showMessage();
int main(){
cout<<"I have a message for you" << endl;
//Call module
showMessage();
cout << endl;
cout <<"That's all folks. :P";
}
//Module showMessage
void showMessage(){
cout << "Hello World";
}