Introduction to Modularity Functionprocedures voidvaluereturning Argumentsparameters Formal argumentsactual
- Slides: 38
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 cosc 175/module. ppt 1
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 cosc 175/module. ppt 2
Module • 1. 2. 3. 4. 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 cosc 175/module. ppt 3
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 cosc 175/module. ppt 4
good names help • Begin with Verb! • Use several short words – Print. Page. Headings – Calc. Sales. Tax – Validate. Input. Date cosc 175/module. ppt 5
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! cosc 175/module. ppt 6
Calling a subprogram/module int main() { … //line 1 Do. Proc 1(); //line 2 …. //line 3 } // end main //line 4 //************** void Do. Proc 1() { //line 5 …. . //line 6 …. . //line 7 } // end Do. Proc 1 //line 8 • • • line 2 Invokes or calls the subprogram Control is transferred to the subprogram: line 5 The code in the subprogram is executed: line 6, 7, 8 At line 8, control returns to statement following call, line 3 1, 2, 5, 6, 7, 8, 3, 4 cosc 175/module. ppt 7
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 cosc 175/module. ppt 8
Do. Stuff Demo. A cosc 175/module. ppt Demo. B Demo. C 9
Writing larger, more complex Programs 1. Define the problem – – Input/Output Processing => list of activities to be performed 2. Plan 1. Modularize => Hierarchy chart • 2. 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 3. 4. Construct code for each successive module in hierarchy chart Desk-check each module in top-down fashion cosc 175/module. ppt 10
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 cosc 175/module. ppt 11
void function Value-returning function return Can return one or more returns one result values Get. Vals(x, y, z) return x * x name Begins with a verb Calc. Square(. . ) Noun Square(. . ) C++ void function void Calc. Square(. . ) stand-alone Calc. Square(. . ) Multipurpose Value returning function float Square(. . ) Call is part of expression x = Square(. . ) Usually mathematical call misc cosc 175/module. ppt 12
void function example // prints lines of *’s where num. Lines specifies // how many lines to print void Print. Lines( int num. Lines ) { int count; for (count = 1; count <=numlines; count++) cout << "********" << endl; } cosc 175/module. ppt 13
function as part of a program int main() { Print. Lines(2); cout << " Welcome Home!“ << endl; Print. Lines(4); return 0; } //************************ // prints lines of *’s // num. Lines specifies how many lines to print void Print. Lines( int num. Lines ) { int count; for (count = 1; count <=numlines; count++) cout << "********" << endl; } //End Print. Lines cosc 175/module. ppt 14
Value returning Function examples //*************** //this function returns the cube of x int Cube (int x) { return x * x; } //************************ //this function returns the maximum of 2 numbers int Max (int num 1, int num 2) { int max; if (num 1 > num 2 ) max = num 1; else max = num 2; return max; } cosc 175/module. ppt 15
Functions in a program void Show Funcs() { int num 1, num 2; cout << “Enter two numbers” cin >> num 1 >> num 2; cout << "The max of " << num 1 << " and << " num 2 " is " << Max(num 1, num 2); cube = Cube(num 1) cout << "The cube of " << num 1 << " is " << Cube(num 1); } //*********************** //this function returns the cube of x int Cube (int x) { return x * x; } //************************ //this function returns the maximum of 2 numbers int Max (int num 1, int num 2) { int max; if (num 1 > num 2 ) max = num 1; else max = num 2; cosc 175/module. ppt return max; } 16
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 – valuereturning • returning one value to be used immediately in an expression – value-returning • when in doubt - use void cosc 175/module. ppt 17
arguments or parameters • In function definition (formal arguments): void Name(type arg 1, type arg 2, …type argn) • In call to function (actual arguments) Name(arg 1, arg 2, …argn) • Arguments must match in number, order and data type but not name • Can be 0, 1, or many arguments cosc 175/module. ppt 18
Formal and Actual Arguments • Formal Argument • In the definition – void Print. Lines(int lines) • Actual Argument • In the call • Print. Lines(2); // could be constant: • Print. Lines(num); //could be variable: • Print. Lines(num+2) //could be Expression • Note: actual parameter and formal arguments may have different names cosc 175/module. ppt 19
Multiple parameters: • matched by position • each param must be declared: void Print. Lines(int num. Lines, char which. Char); Print. Lines(3, ’$’); cosc 175/module. ppt 20
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. cosc 175/module. ppt 21
Input Processing Output char 1 1. Prompt for characters 2. Accept three characters 3. Sort three characters 4. Output three characters Sorted char 1, char 2, char 3 char 2 char 3 cosc 175/module. ppt 22
void Process. Three. Chars() { char 1, char 2, char 3; cout << “Enter three characters”; cin >> char 1 >> char 2 >>char 3; while (!(char 1 == ‘X’ && char 2 == ‘X’ && char 3 == ‘X’)) { if (char 1 > char 2 ) { temp = char 1 char 1 = char 2 char 2 = temp } if (char 2 > char 3) { temp = char 2 char 2 = char 3 char 3 = temp } if (char 1 > char 2) { temp = char 1 char 1 = char 2 char 2 = temp } cout << char 1 << char 2 << char 3; cout << “Enter three characters”; cin >> char 1 >>char 2>>char 3; } cosc 175/module. ppt } 23
void Process. Three. Chars() { char 1, char 2, char 3; cout << “Enter three characters”; cin >> char 1 >> char 2 >>char 3; while (!(char 1 == ‘X’ && char 2 == ‘X’ && char 3 == ‘X’)) { Sort. Three. Characters(char 1, char 2, char 3) cout << “Enter three characters”; cin >> char 1 >>char 2>>char 3; } } cosc 175/module. ppt 24
void Sort. Three. Characters(char& c 1, char& c 2, char& c 3) { char temp; if (char 1 > char 2 ) { temp = char 1 char 1 = char 2 char 2 = temp } if (char 2 > char 3) { temp = char 2 char 2 = char 3 char 3 = temp } if (char 1 > char 2) { temp = char 1 char 1 = char 2 char 2 = temp } } cosc 175/module. ppt 25
two modules - • main module - Read. Three. Characters • submodule - Sort. Three. Characters • after processing is complete, control is returned to main • naming the module - passes control to the module cosc 175/module. ppt 26
• 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 cosc 175/module. ppt 27
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 cosc 175/module. ppt 28
Value Parameters • void Print. Lines(int num. Lines) – num. Lines is formal parameter • void Print. Lines(line. Count); – line. Count is actual parameter • formal parameter receives a copy of the value of line. Count • Print. Lines cannot change line. Count • # of actual params must match # of formal params • types of actual params should match types of formal params – if not, implicit type coercion cosc 175/module. ppt 29
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 cosc 175/module. ppt 30
void Swap. Nums () { 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 cosc 175/module. ppt 31
void Swap. Nums () { 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 cosc 175/module. ppt 32
/// This program outputs an appropriate activity for a given temp. /#include <iostream. h> void Get. Temp( int& ); // Function prototypes void Print. Activity( int ); int main() { int temperature; // The outside temperature Get. Temp(temperature); // Function call Print. Activity(temperature); // Function call return 0; } //*************************** // Prompt for, get, and echo current temperature void Get. Temp( int& temp ) // Reference parameter { cout << "Enter the outside temperature: " << endl; cin >> temp; cout << "The current temperature is " << temp << endl; cosc 175/module. ppt } 33
//*************************** // Given temperature, print appropriate activity void Print. Activity( 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; } cosc 175/module. ppt 34
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 cosc 175/module. ppt 35
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 cosc 175/module. ppt 36
global variables • declared outside all functions int gamma; int main() { } void Some. Func() { . } can be accessed from main or Some. Func cosc 175/module. ppt 37
• 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 cosc 175/module. ppt 38
- Modularity vs efficiency in software testing
- Oop modularity
- Y chart in vlsi
- Modularity and community structure in networks
- Culto casual
- Non formal education meaning
- Unit 3 formal informal and nonformal education
- Peranan kurikulum tidak formal
- Contoh topik dan judul
- Difference between als and formal education
- Komunikasi mendatar
- Contoh falasi formal
- Perbezaan pemimpin formal dan tidak formal
- Aap1 - linguagem e oralidade
- Persamaan paud formal dan nonformal
- Formal email introduction
- Gtg stands for in automata
- Introduction (writing)
- Introduction paragraph format
- Formal report
- How do informational reports and analytical reports differ
- Sincerely on email
- Friendly letter signature
- Semi-formal style of writing
- How to write an email in exam
- Formal letter ending
- Formal letter dear
- Letter writing format with subject
- Scale of inquiry
- Hnc formal charge
- Difference between formal and informal language
- Saludo por correo
- Types of display in visual merchandising
- Semi formal verification
- Formal writing style
- The first principle of business correspondence is
- Congress formal and informal powers
- Registro formal e informal
- Horizontal communication