25
Exposure C++ Chapter VIII Program Modularity and Functions

Exposure C++ Chapter VIII Program Modularity and Functions

Embed Size (px)

Citation preview

Page 1: Exposure C++ Chapter VIII Program Modularity and Functions

Exposure C++

Chapter VIII

Program Modularity and Functions

Page 2: Exposure C++ Chapter VIII Program Modularity and Functions

Divide and Conquer with Modules

Divide a program into modules that focus on a singular task that is manageable. Regardless of size, continue to divide any program segment that is too complex until a manageable task is reached.

A popular computer science saying says:

One task --- one module

Ideally one program module should print on one piece of paper.

If a module becomes too large, divide it into smaller modules.

Page 3: Exposure C++ Chapter VIII Program Modularity and Functions

Function Components

void Example() // function heading{ // start of function body cout << endl; cout << "Hello There";} // end of function body

Page 4: Exposure C++ Chapter VIII Program Modularity and Functions

Local and Global Variables

A local variable is defined inside a function and can only be accessed inside that function.

A global variable is defined outside all the functions and can be used anywhere in the program after the variable definition.

Page 5: Exposure C++ Chapter VIII Program Modularity and Functions

Using Global and Local Variables

Use a global variable when the variable needs to be used in more than one function.

Use a local variable when the variable only needs to be used in one function.

Page 6: Exposure C++ Chapter VIII Program Modularity and Functions

Prototype Notes

Prototypes allow functions to be called without any concern about the order that the functions have been declared.

Prototypes keep the main function at the beginning of the program, since the complete functions (implementations) can now be placed below the main function.

Remember that a prototype is a function heading with a semi-colon at the end.

By convention function prototypes are placed before the main function, and function implementations are placed below the main function body.

Page 7: Exposure C++ Chapter VIII Program Modularity and Functions

Program Development with Stubs

We have now arrived at a very critical topic in computer science. This topic of stubs is embraced by many students and they soon see the benefits. On the other hand, I have seen students who simply refuse to use this wonderful tool. The tool you are about to learn makes program writing quicker because you will have so little trouble identifying error locations.

There is a real irony in this topic. Actually it is not an irony, it is a paradox. Everything you see will appear as if you are taking longer to write a program. Many students reject anything that prolongs the program writing process. But this is only an illusion. It only appears like you take longer, and in reality you save a lot of time.

Page 8: Exposure C++ Chapter VIII Program Modularity and Functions

Stub Definition

A stub is a function without any program statements

// Stub Example

void EnterData( ){

}

Page 9: Exposure C++ Chapter VIII Program Modularity and Functions

// PROG0809.CPP// Using Stubs for program development// Step 1, starts with the main function and include // statements. This step does not compile yet.

#include <iostream.h>#include <iomanip.h>

void main(){ EnterData(); ProcessData(); DisplayData();}

PROG0809.CPP OUTPUT

There is no outputThis program does not compile.

Page 10: Exposure C++ Chapter VIII Program Modularity and Functions

// PROG0810.CPP// Using Stubs for Program Development// Step 2, writing prototypes// This program compiles, but it will not link.

#include <iostream.h>#include <iomanip.h>

void EnterData();void ProcessData();void DisplayData();

void main(){ EnterData(); ProcessData(); DisplayData();}

PROG0810.CPP OUTPUT

There is no output yet.This program compiles,but it does not link.

Page 11: Exposure C++ Chapter VIII Program Modularity and Functions

// PROG0811.CPP// Using Stubs for Program Development// Step 3, writing stubs for each prototype// This step compiles and links.

#include <iostream.h>#include <iomanip.h>

void EnterData();void ProcessData();void DisplayData();

void main(){ EnterData(); ProcessData(); DisplayData();}

void EnterData(){}

void ProcessData(){}

void DisplayData(){}

STUBFUNCTIONS

PROG0811.CPP OUTPUT

This program shows nooutput. It compiles, andlinks, but the stubs areall empty.

Page 12: Exposure C++ Chapter VIII Program Modularity and Functions

// PROG0812.CPP// Using Stubs for Program Development Step 4, creates "glorified stubs" // which helps to showthe global execution sequence of a program.

void EnterData(){ cout << endl << endl; cout << "ENTER DATA FUNCTION" << endl;}

void ProcessData(){ cout << endl << endl; cout << "PROCESS DATA FUNCTION" << endl;}

void DisplayData(){ cout << endl << endl; cout << "DISPLAY DATA FUNCTION" << endl;}

PROG0812.CPP OUTPUT

ENTER DATA FUNCTION

PROCESS DATA FUNCTION

DISPLAY DATA FUNCTION

Page 13: Exposure C++ Chapter VIII Program Modularity and Functions

// PROG0813.CPP// Step 5 completes the EnterData function// This step also defines global variables for EnterData.

double PayRate; double HoursWorked;

void EnterData(){ cout << endl; cout << "ENTER DATA FUNCTION" << endl; cout << endl; cout << "ENTER YOUR HOURLY PAYRATE ===>> "; cin >> PayRate; cout << "ENTER YOUR WEEKLY HOURS ===>> "; cin >> HoursWorked;}

PROG0813.CPP OUTPUT

ENTER DATA FUNCTION

ENTER YOUR HOURLY PAYRATE ===>> 7.75ENTER YOUR WEEKLY HOURS ===>> 25

PROCESS DATA FUNCTION

DISPLAY DATA FUNCTION

Page 14: Exposure C++ Chapter VIII Program Modularity and Functions

// PROG0814.CPP// Step 6 completes the ProcessData function.// This step also defines additional global variables for ProcessData.

double PayRate; double HoursWorked; double GrossPay; double Deductions; double NetPay;

void ProcessData(){ cout << endl; cout << "PROCESS DATA FUNCTION" << endl; GrossPay = PayRate * HoursWorked; Deductions = GrossPay * 0.28; NetPay = GrossPay - Deductions;}

PROG0814.CPP OUTPUT

ENTER DATA FUNCTION

ENTER YOUR HOURLY PAYRATE ===>> 7.75ENTER YOUR WEEKLY HOURS ===>> 25

PROCESS DATA FUNCTION

DISPLAY DATA FUNCTION

Page 15: Exposure C++ Chapter VIII Program Modularity and Functions

// PROG0815.CPP// Step 7 completes the DisplayData function.

void DisplayData(){ cout << endl; cout << "DISPLAY DATA FUNCTION" << endl; cout << endl; cout << setiosflags(ios::fixed); cout << setiosflags(ios::showpoint); cout << setprecision(2); cout << "GROSSPAY: " << GrossPay << endl; cout << "DEDUCTIONS: " << Deductions << endl;

cout << "NETPAY: " << NetPay << endl;}

Page 16: Exposure C++ Chapter VIII Program Modularity and Functions

Final Output of Stub Example

PROG0814.CPP OUTPUT

ENTER DATA FUNCTION

ENTER YOUR HOURLY PAYRATE ===>> 7.75ENTER YOUR WEEKLY HOURS ===>> 25

PROCESS DATA FUNCTION

DISPLAY DATA FUNCTION

GROSSPAY: 193.75DEDUCTIONS: 54.25NETPAY: 139.50

Page 17: Exposure C++ Chapter VIII Program Modularity and Functions

Using the Main Function Properly

// PROG0816.CPP// This program demonstrates modular programming with // only function calls in the program's main function body.

void main(){ Execution(); EnterData(); ComputeMean(); DisplayData(); Terminate();}

Always startwith the

Main Function

Page 18: Exposure C++ Chapter VIII Program Modularity and Functions

Modular Programmingand the

Main Function

The main program function needs to consist of a sequence of function calls, which represent the major program modules.

Function prototypes should be used and placed before the main function.

Page 19: Exposure C++ Chapter VIII Program Modularity and Functions

// PROG0817.CPP// This program demonstrate the use of a frequently used function.

#include <iostream.h>#include <conio.h>

void Skip4();void Continue();void EnterData();void ProcessData();void DisplayData();

void main(){ EnterData(); ProcessData(); DisplayData();}

void Skip4(){ cout << endl << endl << endl << endl;}

Note that function Skip4 isnot used in the main functionbody. It is not one of themain program modules.However, it is convenientlyused by various functions.

Page 20: Exposure C++ Chapter VIII Program Modularity and Functions

void Continue(){ cout << endl << endl; cout << "Press <Enter> to Continue"; getch();}void EnterData(){ Skip4(); cout << "ENTER DATA FUNCTION" << endl; Continue();}void ProcessData(){ Skip4(); cout << "PROCESS DATA FUNCTION" << endl; Continue();}void DisplayData(){ Skip4(); cout << "DISPLAY DATA FUNCTION" << endl; Continue();}

PROG0817.CPP OUTPUT

ENTER DATA FUNCTION

Press <Enter> to Contunue

PROCESS DATA FUNCTION

Press <Enter to Contunue

DISPLAY DATA FUNCTION

Press <Enter> to Continue

Page 21: Exposure C++ Chapter VIII Program Modularity and Functions

// PROG0820.CPP// This program demonstrates using an include file.

#include <iostream.h>#include "PROG0820.INC"

void main(){ One(); Two();}

// PROG0820.INC// This file is included by program PROG0820.CPP

void One(){ cout << endl << endl; cout << "THIS IS PROCEDURE ONE" << endl;}

void Two(){ cout << endl << endl; cout << "THIS IS PROCEDURE TWO" << endl;}

Page 22: Exposure C++ Chapter VIII Program Modularity and Functions

Include File Placement Rule

Include files are compiled at the exact location of the include preprocessor. Normally, the include statement is at the top of the program. However, it can be placed anywhere that a file needs to be included.

User-created include files require quotes like:

#include "UTILITY.INC"

File names are not case-sensitive. They can be lower case, upper case, or a combination. The convention at Berkner High School is to use UPPER CASE for user-created include files.

Page 23: Exposure C++ Chapter VIII Program Modularity and Functions

// PROG0821.CPP// This program demonstrates using a utility library of// of frequently used functions.

#include "UTILITY.INC"

int Nr1,Nr2,Nr3; // three numbers to be averagedfloat Mean; // average of the three numbers

void EnterData();void ComputeMean();void DisplayData();

void main(){ OutputSelection(); Execution(); EnterData(); ComputeMean(); DisplayData(); Terminate();}

<< This statement include the function library

Page 24: Exposure C++ Chapter VIII Program Modularity and Functions

Header and Implementation Files

Function libraries are divided into two separate files:

Header files, which stores all the function heading proto-types and other include libraries.

Implementation files, which stores the details of the complete function implementations

The header file ends with .H and has a name like UTILITY.H and includes the implementation file.

The implementation uses the same identifier as the header file and has a name like UTILITY.CPP.

Page 25: Exposure C++ Chapter VIII Program Modularity and Functions

// PROG0822.CPP// This program demonstrates how to use the utility library// by including the header file, which in turn includes the// implementation file.

#include "UTILITY.H"

int Nr1,Nr2,Nr3; // three numbers to be averagedfloat Mean; // average of the three numbers

void EnterData();void ComputeMean();void DisplayData();

void main(){ OutputSelection(); Execution(); EnterData(); ComputeMean(); DisplayData(); Terminate();}

The Complete Utility Library

UTILITY.H file & UTILITY.CPP file are

Shown on computer and in your book