Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exposure C++ Chapter VIII Program Modularity and Functions.

Similar presentations


Presentation on theme: "Exposure C++ Chapter VIII Program Modularity and Functions."— Presentation transcript:

1

2 Exposure C++ Chapter VIII Program Modularity and Functions

3 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.

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

5 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.

6 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.

7 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.

8 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.

9 Stub Definition A stub is a function without any program statements // Stub Example void EnterData( ) { }

10 // PROG0809.CPP // Using Stubs for program development // Step 1, starts with the main function and include // statements. This step does not compile yet. #include void main() { EnterData(); ProcessData(); DisplayData(); } PROG0809.CPP OUTPUT There is no output This program does not compile.

11 // PROG0810.CPP // Using Stubs for Program Development // Step 2, writing prototypes // This program compiles, but it will not link. #include 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.

12 // PROG0811.CPP // Using Stubs for Program Development // Step 3, writing stubs for each prototype // This step compiles and links. #include void EnterData(); void ProcessData(); void DisplayData(); void main() { EnterData(); ProcessData(); DisplayData(); } void EnterData() { } void ProcessData() { } void DisplayData() { } STUB FUNCTIONS PROG0811.CPP OUTPUT This program shows no output. It compiles, and links, but the stubs are all empty.

13 // 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

14 // 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 > "; cin >> PayRate; cout > "; cin >> HoursWorked; } PROG0813.CPP OUTPUT ENTER DATA FUNCTION ENTER YOUR HOURLY PAYRATE ===>> 7.75 ENTER YOUR WEEKLY HOURS ===>> 25 PROCESS DATA FUNCTION DISPLAY DATA FUNCTION

15 // 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.75 ENTER YOUR WEEKLY HOURS ===>> 25 PROCESS DATA FUNCTION DISPLAY DATA FUNCTION

16 // 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 ; }

17 Final Output of Stub Example PROG0814.CPP OUTPUT ENTER DATA FUNCTION ENTER YOUR HOURLY PAYRATE ===>> 7.75 ENTER YOUR WEEKLY HOURS ===>> 25 PROCESS DATA FUNCTION DISPLAY DATA FUNCTION GROSSPAY: 193.75 DEDUCTIONS: 54.25 NETPAY: 139.50

18 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 start with the Main Function

19 Modular Programming and 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.

20 // PROG0817.CPP // This program demonstrate the use of a frequently used function. #include 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 is not used in the main function body. It is not one of the main program modules. However, it is conveniently used by various functions.

21 void Continue() { cout << endl << endl; cout 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 to Contunue PROCESS DATA FUNCTION Press <Enter to Contunue DISPLAY DATA FUNCTION Press to Continue

22 // PROG0820.CPP // This program demonstrates using an include file. #include #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; }

23 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.

24 // 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 averaged float 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

25 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.

26 // 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 averaged float 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


Download ppt "Exposure C++ Chapter VIII Program Modularity and Functions."

Similar presentations


Ads by Google