380 likes | 422 Views
Introduction to Modularity. Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by value/pass by reference Scope: global/local/name precedence. Modularity. Most programs so far have been simple - less than one page in length
E N D
Introduction to Modularity • Function/procedures • void/value-returning • Arguments/parameters • Formal arguments/actual arguments • Pass by value/pass by reference • Scope: global/local/name precedence cosc175/module.ppt
Modularity • Most programs so far have been simple - less than one page in length • In reality, most problems are not so simple • top-down design - identify first the major tasks and further subtasks within them • Modularity – breaking a program into subprograms cosc175/module.ppt
Module • section of an algorithm which is dedicated to a single function • performs one single function • single entry, single exit • short enough to be easily read and modified • long enough to perform function cosc175/module.ppt
Modules • all tasks can be subdivided and further subdivided into subtasks • goal => can easily construct pseudocode for each subtask • each module constructed and tested as unit • Black Box • Stubbing in (write dummy modules) • Always have something working cosc175/module.ppt
good names help • Begin with Verb! • Use several short words • PrintPageHeadings • CalcSalesTax • ValidateInputDate cosc175/module.ppt
Mainline or driver • call subtasks • should show the main processing functions, the order they should be performed • easy to read • manageable length • Should include a loop! cosc175/module.ppt
int main() { … //line1 DoProc1(); //line2 …. //line3 } // end main //line4 //*************************** void DoProc1() { //line5 ….. //line6 ….. //line7 } // end DoProc1 //line8 line2 Invokes or calls the subprogram Control is transferred to the subprogram: line5 The code in the subprogram is executed: line 6,7,8 At line8, control returns to statement following call, line 3 1,2,5,6,7,8,3,4 Calling a subprogram/module cosc175/module.ppt
Hierarchy Chart shows who calls whom • illustrates structure of the solution to a problem • i.e organization chart for a company • shows top-down design, communication flow • One block for each module • Program name in first block, begin with verb cosc175/module.ppt
Writing larger, more complex Programs • Define the problem • Input/Output • Processing => list of activities to be performed • Plan • Modularize => Hierarchy chart • Group list of activities into modules • Construct code for main-line: Initial processing Loop processing //Contains calls to major processing modules(stubs) END Loop Final Processing • Construct code for each successive module in hierarchy chart • Desk-check each module in top-down fashion cosc175/module.ppt
advantages of well structured programs: • can easily be changed, updated and maintained • division into tasks makes them easy to understand • easy to construct • can test modules individually • easy to test and debug - easier to isolate errors • can have each module print input and output • more reliable - fewer bugs cosc175/module.ppt
void function example // prints lines of *’s where numLines specifies // how many lines to print void PrintLines( int numLines ) { int count; for (count = 1; count <=numlines; count++) cout << "***************" << endl; } cosc175/module.ppt
function as part of a program int main() { PrintLines(2); cout << " Welcome Home!“ << endl; PrintLines(4); return 0; } //*********************************************** // prints lines of *’s // numLines specifies how many lines to print void PrintLines( int numLines ) { int count; for (count = 1; count <=numlines; count++) cout << "***************" << endl; } //End PrintLines cosc175/module.ppt
Value returning Function examples //****************************** //this function returns the cube of x int Cube (int x) { return x * x * x; } //************************************************ //this function returns the maximum of 2 numbers int Max (int num1,int num2) { int max; if (num1 > num2 ) max = num1; else max = num2; return max; } cosc175/module.ppt
Functions in a program void Show Funcs() { int num1,num2; cout << “Enter two numbers” cin >> num1 >> num2; cout << "The max of " << num1 << " and << " num2 " is " << Max(num1,num2); cube = Cube(num1) cout << "The cube of " << num1 << " is " << Cube(num1); } //********************************************** //this function returns the cube of x int Cube (int x) { return x * x * x; } //************************************************ //this function returns the maximum of 2 numbers int Max (int num1,int num2) { int max; if (num1 > num2 ) max = num1; else max = num2; return max; } cosc175/module.ppt
When to use void versus value-returning • When returning more than one value - use void • when I/O is required - use void • returning one Boolean value – value-returning • returning one value to be used immediately in an expression – value-returning • when in doubt - use void cosc175/module.ppt
arguments or parameters • In function definition (formal arguments): void Name(type arg1, type arg2,…type argn) • In call to function (actual arguments) Name(arg1,arg2,…argn) • Arguments must match in number, order and data type but not name • Can be 0,1, or many arguments cosc175/module.ppt
Formal and Actual Arguments • Formal Argument • In the definition • void PrintLines(int lines) • Actual Argument • In the call • PrintLines(2); // could be constant: • PrintLines(num); //could be variable: • PrintLines(num+2) //could be Expression • Note: actual parameter and formal arguments may have different names cosc175/module.ppt
Multiple parameters: • matched by position • each param must be declared: void PrintLines(int numLines,char whichChar); PrintLines(3,’$’); cosc175/module.ppt
Example • Read three characters: • Design a solution algorithm which will: prompt a terminal operator for three characters, accept those characters as input, sort them in ascending sequence and output them to the screen. The algorithm is to continue to accept characters until ‘XXX’ is entered. cosc175/module.ppt
void ProcessThreeChars() { char char1,char2,char3; cout << “Enter three characters”; cin >> char1 >> char2 >>char3; while (!(char1 == ‘X’ && char2 == ‘X’ && char3 == ‘X’)) { if (char1 > char2 ) { temp = char1 char1 = char2 char2 = temp } if (char2 > char3) { temp = char2 char2 = char3 char3 = temp } if (char1 > char2) { temp = char1 char1 = char2 char2 = temp } cout << char1 << char2 << char3; cout << “Enter three characters”; cin >> char1 >>char2>>char3; } } cosc175/module.ppt
void ProcessThreeChars() { char char1,char2,char3; cout << “Enter three characters”; cin >> char1 >> char2 >>char3; while (!(char1 == ‘X’ && char2 == ‘X’ && char3 == ‘X’)) { SortThreeCharacters(char1,char2,char3) cout << “Enter three characters”; cin >> char1 >>char2>>char3; } } cosc175/module.ppt
void SortThreeCharacters(char& c1, char& c2,char& c3) { char temp; if (char1 > char2 ) { temp = char1 char1 = char2 char2 = temp } if (char2 > char3) { temp = char2 char2 = char3 char3 = temp } if (char1 > char2) { temp = char1 char1 = char2 char2 = temp } } cosc175/module.ppt
two modules - • main module - ReadThreeCharacters • submodule - SortThreeCharacters • after processing is complete, control is returned to main • naming the module - passes control to the module cosc175/module.ppt
module invokes or calls subordinate modules • calling module • called module - return control to calling module upon completion of its task • module may only call modules that are at the same level and immediately below it • exception - library or utility modules (later) • in general, no module should have more than seven modules subordinate to it cosc175/module.ppt
Parameters • in, in out, out • in - value parameters • pass by value • function receives a copy of the value • in out, out - reference parameters • pass by reference • attach ampersand to data type, int& param • function receives the address of the actual parameter cosc175/module.ppt
Value Parameters • void PrintLines(int numLines) • numLines is formal parameter • void PrintLines(lineCount); • lineCount is actual parameter • formal parameter receives a copy of the value of lineCount • PrintLines cannot change lineCount • # of actual params must match # of formal params • types of actual params should match types of formal params • if not, implicit type coercion cosc175/module.ppt
Reference Parameters • use & (C++ convention) • function can change the value • pass by reference • location of parameter is passed • only one copy of the value • only variables can be passed as an actual parameter to a reference parameter cosc175/module.ppt
void SwapNums () { int x,y; x = 5; y = 10; cout << "Originally x = “ << x << " and y = “ << y; Swap(x,y); cout << "Now x = " << x << " and y = " << y; } void Swap(int u,int v) int temp; temp = u; u = v; v = temp; } Originally x = 5 and y = 10 Now x = 5 and y = 10 cosc175/module.ppt
void SwapNums () { int x,y; x = 5; y = 10; cout << "Originally x = “ << x << " and y = “ << y; Swap(x,y); cout << "Now x = " << x << " and y = " << y; } void Swap(int& u,int& v) int temp; temp = u; u = v; v = temp; } Originally x = 5 and y = 10 Now x = 10 and y = 5 cosc175/module.ppt
/// This program outputs an appropriate activity for a given temp. /#include <iostream.h> void GetTemp( int& ); // Function prototypes void PrintActivity( int ); int main() { int temperature; // The outside temperature GetTemp(temperature); // Function call PrintActivity(temperature); // Function call return 0; } //****************************************************** // Prompt for, get, and echo current temperature void GetTemp( int& temp ) // Reference parameter { cout << "Enter the outside temperature:" << endl; cin >> temp; cout << "The current temperature is " << temp << endl; } cosc175/module.ppt
//****************************************************** // Given temperature, print appropriate activity void PrintActivity( int temp ) // Value parameter { cout << "The recommended activity is "; if (temp > 85) cout << "swimming." << endl; else if (temp > 70) cout << "tennis." << endl; else if (temp > 32) cout << "golf." << endl; else if (temp > 0) cout << "skiing." << endl; else cout << "dancing." << endl; } cosc175/module.ppt
Scope • area of program where a variable is visible • global - visible to all modules • declared in or above the main module • local - visible only to the module in which it appears • side effects - cross-communication of a module with other parts of a program cosc175/module.ppt
Local Variables • each function is a block • any function can declare variables within block • accessible only within the block they are declared • occupy space only when the function is executing • when the function is invoked - local vars created • when function is finished - local vars destroyed cosc175/module.ppt
global variables • declared outside all functions int gamma; int main() { } void SomeFunc() { . } can be accessed from main or SomeFunc cosc175/module.ppt
It is possible to have identifiers with the same name both in the main program and the subprogram: • name precedence - a local identifier in a module takes precedence over a global identifier with the same spelling in any reference the procedure makes to the identifier cosc175/module.ppt