Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by.

Similar presentations


Presentation on theme: "Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by."— Presentation transcript:

1 cosc175/module.ppt1 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

2 cosc175/module.ppt2 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

3 cosc175/module.ppt3 Module section of an algorithm which is dedicated to a single function 1.performs one single function 2.single entry, single exit 3.short enough to be easily read and modified 4.long enough to perform function

4 cosc175/module.ppt4 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

5 cosc175/module.ppt5 good names help Begin with Verb! Use several short words –PrintPageHeadings –CalcSalesTax –ValidateInputDate

6 cosc175/module.ppt6 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!

7 cosc175/module.ppt7 Calling a subprogram/module 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

8 cosc175/module.ppt8 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

9 cosc175/module.ppt9 DoStuff DemoADemoBDemoC

10 cosc175/module.ppt10 Writing larger, more complex Programs 1.Define the problem –Input/Output –Processing => list of activities to be performed 2. Plan 1.Modularize => Hierarchy chart Group list of activities into modules 2. Construct code for main-line: Initial processing Loop processing //Contains calls to major processing modules(stubs) END Loop Final Processing 3.Construct code for each successive module in hierarchy chart 4.Desk-check each module in top-down fashion

11 cosc175/module.ppt11 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

12 cosc175/module.ppt12 void functionValue-returning function returnCan return one or more values GetVals(x,y,z) returns one result return x * x nameBegins with a verb CalcSquare(..) Noun Square(..) C++void function void CalcSquare(..) Value returning function float Square(..) callstand-alone CalcSquare(..) Call is part of expression x = Square(..) miscMultipurposeUsually mathematical

13 cosc175/module.ppt13 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; }

14 cosc175/module.ppt14 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

15 cosc175/module.ppt15 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; }

16 cosc175/module.ppt16 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; }

17 cosc175/module.ppt17 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

18 cosc175/module.ppt18 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

19 cosc175/module.ppt19 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

20 cosc175/module.ppt20 Multiple parameters: matched by position each param must be declared: void PrintLines(int numLines,char whichChar); PrintLines(3,’$’);

21 cosc175/module.ppt21 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.

22 cosc175/module.ppt22 InputProcessingOutput char11. Prompt for characters Sorted char1,char2,char3 char22. Accept three characters char33. Sort three characters 4.Output three characters

23 cosc175/module.ppt23 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; }

24 cosc175/module.ppt24 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; }

25 cosc175/module.ppt25 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 }

26 cosc175/module.ppt26 two modules - main module - ReadThreeCharacters submodule - SortThreeCharacters after processing is complete, control is returned to main naming the module - passes control to the module

27 cosc175/module.ppt27 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

28 cosc175/module.ppt28 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

29 cosc175/module.ppt29 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

30 cosc175/module.ppt30 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

31 cosc175/module.ppt31 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

32 cosc175/module.ppt32 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

33 cosc175/module.ppt33 /// This program outputs an appropriate activity for a given temp. /#include 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; }

34 cosc175/module.ppt34 //****************************************************** // 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; }

35 cosc175/module.ppt35 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

36 cosc175/module.ppt36 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

37 cosc175/module.ppt37 global variables declared outside all functions int gamma; int main() { } void SomeFunc() {. } can be accessed from main or SomeFunc

38 cosc175/module.ppt38 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


Download ppt "Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by."

Similar presentations


Ads by Google