Top Banner
cosc175/module.ppt 1 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
38

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

Dec 13, 2015

Download

Documents

Sydney McCoy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by.

cosc175/module.ppt 1

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

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

cosc175/module.ppt 2

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

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

cosc175/module.ppt 3

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

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

cosc175/module.ppt 4

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

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

cosc175/module.ppt 5

good names help

• Begin with Verb!

• Use several short words– PrintPageHeadings– CalcSalesTax– ValidateInputDate

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

cosc175/module.ppt 6

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!

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

cosc175/module.ppt 7

Calling a subprogram/module

int main(){

… //line1DoProc1(); //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

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

cosc175/module.ppt 8

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

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

cosc175/module.ppt 9

int main() { . . DemoC(); DemoB(); DemoA(); return 0; } //*************** void DemoA() { . . } //**************** void DemoB() { . . } //**************** void DemoC() { . . }

DoStuff

DemoA DemoB DemoC

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

cosc175/module.ppt 10

Writing larger, more complex Programs1. Define the problem

– Input/Output– Processing => list of activities to be performed

2. Plan1. Modularize => Hierarchy chart

• Group list of activities into modules

2. Construct code for main-line:Initial processingLoop

processing //Contains calls to major processing modules(stubs)END LoopFinal Processing

3. Construct code for each successive module in hierarchy chart 4. Desk-check each module in top-down fashion

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

cosc175/module.ppt 11

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

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

cosc175/module.ppt 12

 

void function Value-returning function

return Can return one or more values

GetVals(x,y,z)

returns one result

return x * x

name Begins with a verb

CalcSquare(..)

Noun

Square(..)

C++ void function

void CalcSquare(..)

Value returning function

float Square(..)

call stand-alone

CalcSquare(..)

Call is part of expression

x = Square(..)

misc Multipurpose Usually mathematical

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

cosc175/module.ppt 13

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;

}

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

cosc175/module.ppt 14

function as part of a programint main() { PrintLines(2);

cout << " Welcome Home!“ << endl;PrintLines(4);return 0;

}//***********************************************// prints lines of *’s// numLines specifies how many lines to printvoid PrintLines( int numLines ){ int count;   for (count = 1; count <=numlines; count++) cout << "***************" << endl;} //End PrintLines

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

cosc175/module.ppt 15

Value returning Function examples

//******************************//this function returns the cube of xint Cube (int x){ return x * x * x;}

//************************************************  //this function returns the maximum of 2 numbersint Max (int num1,int num2){ int max;

if (num1 > num2 ) max = num1;

else max = num2;

return max;}

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

cosc175/module.ppt 16

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 xint  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;}

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

cosc175/module.ppt 17

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

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

cosc175/module.ppt 18

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

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

cosc175/module.ppt 19

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

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

cosc175/module.ppt 20

Multiple parameters:

• matched by position

• each param must be declared:void PrintLines(int numLines,char whichChar);

PrintLines(3,’$’);

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

cosc175/module.ppt 21

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.

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

cosc175/module.ppt 22

Input Processing Output

char1 1. Prompt for characters

Sorted char1,char2,char3

char2 2. Accept three characters

char3 3. Sort three characters

4.Output three characters

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

cosc175/module.ppt 23

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

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

cosc175/module.ppt 24

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

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

cosc175/module.ppt 25

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

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

cosc175/module.ppt 26

two modules -

• main module - ReadThreeCharacters

• submodule - SortThreeCharacters

• after processing is complete, control is returned to main

• naming the module - passes control to the module

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

cosc175/module.ppt 27

• 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

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

cosc175/module.ppt 28

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

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

cosc175/module.ppt 29

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

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

cosc175/module.ppt 30

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

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

cosc175/module.ppt 31

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 = 10Now x = 5 and y = 10

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

cosc175/module.ppt 32

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 = 10Now x = 10 and y = 5

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

cosc175/module.ppt 33

/// This program outputs an appropriate activity for a given temp./#include <iostream.h>void GetTemp( int& ); // Function prototypesvoid 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 temperaturevoid GetTemp( int& temp ) // Reference parameter{ cout << "Enter the outside temperature:" << endl; cin >> temp; cout << "The current temperature is " << temp << endl;}

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

cosc175/module.ppt 34

//******************************************************// Given temperature, print appropriate activityvoid 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;}

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

cosc175/module.ppt 35

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

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

cosc175/module.ppt 36

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

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

cosc175/module.ppt 37

global variables

• declared outside all functionsint gamma;

int main(){

}void SomeFunc(){ .}

can be accessed from main or SomeFunc

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

cosc175/module.ppt 38

• 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