Beruflich Dokumente
Kultur Dokumente
Modular Programming
Zarina Tukiran
Dept. of Computer Engineering Faculty of Electrical & Electronic Engineering Email: zarin@uthm.edu.my Office No.: 07-4537566
MODULAR PROGRAMMING
Objectives :
understand the programming. concept of modularity in
introduce the common functions available in the C++ standard library. able to write a user-defined function in C++.
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 be broke into manageable pieces (modules / subroutines) called functions.
MODULAR PROGRAMMING
MODULAR PROGRAMMING
MODULAR PROGRAMMING
MODULAR PROGRAMMING
Categories of Functions
There are two types of functions: Predefined function User-defined function
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 the functions 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.
MODULAR PROGRAMMING
MODULAR PROGRAMMING
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 userdefined 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 11 return a value. Sem II Session 2012/2013
MODULAR PROGRAMMING
Function Elements
There are three function elements: Function definition Function call Function prototype
12
MODULAR PROGRAMMING
13
MODULAR PROGRAMMING
int add (int a, int b) { Formal parameter list int sum; sum = a + b; return sum; }
Sem II Session 2012/2013
MODULAR PROGRAMMING
double circleArea( ) { Empty formal parameter list doube r, area; const double PI=3.142; cout<<Enter radius:; cin>>r; area=PI*(r*r); return (area); Sem II Session 2012/2013 }
15
MODULAR PROGRAMMING
Function name
char letter
int n
void decide (char letter, int n) { if ((letter == A) && (n==100)) cout<<Good; else cout<<Try it again! }
Sem II Session 2012/2013
decide
16
MODULAR PROGRAMMING
void circleArea( ) { Empty formal Parameter list doube r, area; const double PI=3.142; cout<<Enter radius:; cin>>r; area=PI*(r*r); cout<<Area circle is << area; Sem II Session 2012/2013 }
circleArea
17
MODULAR PROGRAMMING
MODULAR PROGRAMMING
19
MODULAR PROGRAMMING
view (x);
Example 7: (call-by-reference)
20
MODULAR PROGRAMMING
21
MODULAR PROGRAMMING
22
MODULAR PROGRAMMING
MODULAR PROGRAMMING
j=op(a,b);
mul (z); The function prototypes are as follows: float op (float a, float b);
Sem II Session 2012/2013
#include <iostream>
MODULAR PROGRAMMING
float j;
MODULAR PROGRAMMING
To insert the custom header file in a C++ program using pre-processor directive. Example: #include myfunctionlib.h
26
MODULAR PROGRAMMING
Practice
27
MODULAR PROGRAMMING
MODULAR PROGRAMMING
29
MODULAR PROGRAMMING
Enter integer value, the program exit if the input is 0. 1 2 -1 3 0 The number of positives is 3 The number of negatives is 1 The total is 5 The average is 1.25
Sem II Session 2012/2013 30
MODULAR PROGRAMMING
Exercise 3: (cont)
Write C++ program for each problem statements. Implement modular programming in your program.
Problem Statement 2: Conversion from Celsius to Fahrenheit. Write a program that able to print out a temperature conversion table form degrees in Celsius to degree in Fahrenheit when the temperature varies in the range of -10 to 40 degrees in Celsius with an incremental step size of 1 degree. The relationship is as follows:
f=(9/5)c+32
31
MODULAR PROGRAMMING
Exercise 3: (cont)
Write C++ program for each problem statements. Implement modular 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 each students 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.
32