View
224
Download
1
Category
Preview:
Citation preview
7/30/2019 Ch4 Modular Programming
1/32
MODULAR PROGRAMMING
Modular Programming
Zarina TukiranDept. of Computer Engineering
Faculty of Electrical & Electronic EngineeringEmail: zarin@uthm.edu.my
Office No.: 07-4537566
1Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
2/32
MODULAR PROGRAMMING
2
Objectives :
understand the concept of modularity in
programming.
introduce the common functions available in the C++standard library.
able to write a user-defined function in C++.
Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
3/32
MODULAR PROGRAMMING
Introduction
The C++ program describes in Chapter 1 through
Chapter 3 uses only the function main; the programming
instructions are packed into one function.
This technique, however, is good for a short program.
For large program, it is not practical to put the entire
programming instructions into one function. It must bebroke into manageable pieces (modules / subroutines)
called functions.
3Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
4/32
MODULAR PROGRAMMING
Hierarchical modular design technique
Modularization (divide and conquer)
The complexity of design in broken down (divided) into a
hierarchy of modules/routines; from general (top) tospecific (bottom).
Benefits:
Focus on a single module at a time. Create customised low-level modules for design
reuse.
Avoid the repetition of writing out the same segment
of code. 4Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
5/32
MODULAR PROGRAMMING
Hierarchical modular design technique (cont)
Top-down decomposition partitions the system into
smaller subsystems up to a level which the subsystems
can be realised.
Bottom-up composition integrates and connects
available modules to form bigger, more complex
subsystems.
5Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
6/32
MODULAR PROGRAMMING
6Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
7/32
MODULAR PROGRAMMING
Categories of Functions
There are two types of functions:
Predefined function
User-defined function
7Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
8/32
MODULAR PROGRAMMING
Pre-defined function
Review a concept from a algebra course.
In algebra, a function can be considered a rule or
correspondence between values, called thefunctions
arguments, and the unique value of the function
associated with the arguments.
Thus, if f(x)=2x+5, then f(1)=7, f(2)=9, and f(3)=11,where 1,2 and 3 are the arguments of f, and 7, 9 and 11
are the corresponding values of the function f.
8Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
9/32
MODULAR PROGRAMMING
Pre-defined function (cont)
In C++, the concept of a function, either predefined or user-
defined, is similar to that of a function in algebra.
For example, every function has a name and depending
on the values specified by the user, it does some
computation. Some of the predefined mathematical
functions arepow(x,y), sqrt(x) andfloor(x).
In C++, predefined functions are organized into separate
libraries. For example, the header file iostream contains I/O
functions and the header file cmath contains math functions.
9Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
10/32
MODULAR PROGRAMMING
Pre-defined function (cont)
Table below lists some of the predefined functions, the name
of the header file in which each functions specification can be
found, the data type of the parameters and the function type.
The function type is the data type of the final value returned
by the function.
Function Header File PurposeParameter(s)
typeResult
ceil (x) Returns the smallest whole number that is
not less than x: ceil (56.34)=57.0double double
cos (x)
Returns the cosine of angle x: cos(0.0)=1.0 double
(radians) double
exp (x) Returns ex, where e=2.718: exp
(1.0)=2.71828double double
tolower(x) Returns the lowercase value of x if x is
uppercase; otherwise, returns xint int
topupper(x) Returns the uppercase value of x if x is
lowercase; otherwise, returns x
int int
10Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
11/32
MODULAR PROGRAMMING
User-defined function
C++ does not provide every function that we will ever need
and designers cannot possibly know a users specific needs,
we need to write our own functions this is called user-
defined functions.
User-defined functions in C++ are classified into two
categories:
value-returning functions functions that have a
return type. These functions return a value of a specific
data type using the return statement.
void functions functions that do not have a return
type. These functions do not use a return statement to
return a value. 11Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
12/32
MODULAR PROGRAMMING
Function Elements
There are three function elements:
Function definition
Function call
Function prototype
12Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
13/32
MODULAR PROGRAMMING
Function Elements: Function Definition
General syntax
returnValueType funct ionName(formal parameters list)
{/*function body*/
}
13Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
14/32
MODULAR PROGRAMMING
Function Elements: Function Definition (cont)
Example 1 (value-returning function)
int add(int a, int b)
{
int sum;
sum = a + b;
return sum;
}
add
int a int b
int sum
Function name
Formal parameter list
Return Value Type
Pictorial view of value-
returning function
14Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
15/32
MODULAR PROGRAMMING
Function Elements: Function Definition (cont)
Example 2 (value-returning function)
double circ leArea( )
{
doube r, area;
const double PI=3.142;
coutr;
area=PI*(r*r);
return (area);
}
circleArea
double area
Function name
Empty formal parameter list
Return Value TypePictorial view of value-
returning function
15Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
16/32
MODULAR PROGRAMMING
Function Elements: Function Definition (cont)
Example 3 (void function)
void decide(char letter, int n)
{
if ((letter == A) && (n==100))
cout
7/30/2019 Ch4 Modular Programming
17/32
MODULAR PROGRAMMING
Function Elements: Function Definition (cont)
Example 4 (void function)
void circ leArea( )
{
doube r, area;
const double PI=3.142;
coutr;
area=PI*(r*r);
cout
7/30/2019 Ch4 Modular Programming
18/32
MODULAR PROGRAMMING
Function Elements: Function Call
A function is executed if it is calledorinvoked.
In C++, the function calls can be called by :
The main function
User-definedfunction user-defined function call other user-defined
function
Call itself (recursion method)
There are two ways to call/invoke a function: call-by-value
call-by-reference
18Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
19/32
MODULAR PROGRAMMING
Function Elements: Function Call
General Syntax:
(a) use the following format forvalue-returning function.
variableName = functionName (list of actual parameters);
Example 5: (call-by-value)
y = findMin (a, b);
19Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
20/32
MODULAR PROGRAMMING
Function Elements: Function Call (cont)
(b) use the following format forvoid function
functionName (list of actual parameters);
Example 6: (call-by-value)
view (x);
Example 7: (call-by-reference)
calculate (&num, &x, &y);
20Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
21/32
MODULAR PROGRAMMING
Function Elements: Function Call (cont)
Call-by-value
The value holds by actual parameter is copied to the
formal parameter.
If the variable within the function is modified then uponreturn from the function, the value of actual variable is
not modified.
Call-by-reference The address of actual parameter is copied to the formal
parameter.
If the variable within the function is modified then the
value of actual variable is modified. 21Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
22/32
MODULAR PROGRAMMING
Function Elements: Function Prototype
The function prototype
is an important feature.
tells the compiler what type of value the function returns,
number and types of parameters, and order in whichthese parameters are expected.
must added to a C++ program before the main function,
if call the function before defining it.
is not needed in a C++ program if the function is calledafterfunction definition.
22Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
23/32
MODULAR PROGRAMMING
Function Elements: Function Prototype (cont)
General syntax
returnValueType funct ionName(formal parameters list) ;
Example:
bool accept(char let1, char let2) ;
More examples:
void display() ;
void gName(float m) ;
doub letemp(float t1, float t2) ;
float dSeries(float r[]) ;23Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
24/32
MODULAR PROGRAMMING
Given the following.Lets say you have the following function definition
and the function calls are as follows.
j=op(a,b);
mul (z);
The function prototypes are as follows:
float op (float a, float b); void mul (int z);
void mul (int z)
{ int m;
m=z*5;
cout
7/30/2019 Ch4 Modular Programming
25/32
MODULAR PROGRAMMING
Thus,
the
fullprogra
m:
#include
using namespace std;
float oper (float a, float b);//function prototype of op
void mul (int z);//function prototype of mul
//===========main function======================
void main(){
floatj;
j=oper (2.4,2.0); //function call to op
cout
7/30/2019 Ch4 Modular Programming
26/32
MODULAR PROGRAMMING
ADDITIONAL: Custom Header File
Programmer can create custom header files
should end in .h
e.g. myfunct ion l ib .h
To insert the custom header file in a C++ program using
pre-processor directive. Example:
#include myfunctionlib.h
26Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
27/32
MODULAR PROGRAMMING
Practice
27Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
28/32
MODULAR PROGRAMMING
Exercise 1: Familiarise the concept
Write C++ statements that provide a function definition, calland prototype for the following description.
1. A user-defined function named myWish which able to
display I want grade A for C++ only once.2. A user-defined function named repeatWish which able
to display I want grade A for C++ 10 times.
3. A user-defined function named minus which able to find
the subtraction of three numbers.4. A user-defined function named FindMaxwhich able to
determine the highest value of two integer numbers.
28Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
29/32
MODULAR PROGRAMMING
Exercise 2: Implementation of concept in the
program
Now, integrate all user-defined function (Exercise 1) in a
single C++ program.
29Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
30/32
MODULAR PROGRAMMING
Exercise 3: More Programming
Write C++ program for each problem statements. Implementmodular programming in your program.
Problem Statement 1: Counti ng posit ive and negative numbers and computi ng the
average of numbers.
Write a program that reads an unspecified number of integers, determines
how many positive and negative values have been read, and computes the
total and average of the input values (not counting zeros). Your program
ends with the input 0. Display the average as a floating-point number. Here is
a sample run:
Enter integer value, the program exit if the input is 0.
1 2 -1 3 0The number of positives is 3
The number of negatives is 1
The total is 5
The average is 1.25
30Sem II Session 2012/2013
7/30/2019 Ch4 Modular Programming
31/32
7/30/2019 Ch4 Modular Programming
32/32
MODULAR PROGRAMMING
Exercise 3: (cont)
Write C++ program for each problem statements. Implementmodular programming in your program.
Problem Statement 3:Finding the highest score
Write a program that prompts the user to enter the number of students
and eachstudents
score, and displays the highest score.
Problem Statement 4:Finding the highest and the lowest score
Write a program that prompts the user to enter the number of students
and each students score, and displays the highest and the lowest score.
32Sem II Session 2012/2013
Recommended