Introduction to Modularity - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Introduction to Modularity

Description:

shows top-down design, communication flow. One block for each module ... Desk-check each module in top-down fashion. cosc175/module.ppt. 11 ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 44
Provided by: unkn778
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Modularity


1
Introduction to Modularity
  • Function/procedures
  • Arguments/parameters
  • Formal arguments/actual arguments
  • Pass by value/pass by reference
  • Scope global/local/name precedence

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 then further subtasks within them
  • Modularity breaking a program into subprograms

3
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

4
Modules
  • all tasks can be subdivided and further
    subdivided into subtasks
  • goal gt 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
good names help
  • Begin with Verb!
  • Use several short words
  • PrintPageHeadings
  • CalcSalesTax
  • ValidateInputDate

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!

7
Calling a subprogram/module
  • MainProg
  • //line1
  • Call DoProc1() //line2
  • . //line3
  • End MainProg //line4
  • //
  • 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,43,4

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

9
(No Transcript)
10
Writing larger, more complex Programs
  • Define the problem
  • Input/Output
  • Processing gt list of activities to be performed
  • Plan
  • Modularize gt Hierarchy chart
  • Group list of activities into modules
  • Construct pseudocode for main-line
  • Initial processing
  • Loop
  • processing //Contains calls to major processing
    modules(stubs)
  • END Loop
  • Final Processing
  • Construct pseudocode for each successive module
    in hierarchy chart
  • Desk-check each module in top-down fashion

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

12
  •  

13
Procedure example
  • // prints lines of s where numLines specifies
    how many lines to print
  • Procedure PrintLines( integer numLines )
  • integer count // Loop control variable
  •   do count 1 to numlines
  • Display "" ,newline
  • end do
  • End PrintLines

14
Procedure as part of a program
  • Program PrintWelcome
  • Call PrintLines(2)
  • Display " Welcome Home!"
  • Call PrintLines(4)
  • End PrintWelcome
  • //
  • // prints lines of s where numLines specifies
    how many lines to print
  • Procedure PrintLines( integer numLines )
  • integer count // Loop control variable
  •   do count 1 to numlines
  • Display "" ,newline
  • end do
  • End PrintLines

15
Function examples
  • //
  • //this function returns the cube of x
  • Integer  FUNCTION Cube (integer x)
  • return x x x
  • END Cube
  • //
  •   //this function returns the maximum of 2
    numbers
  • Integer  FUNCTION Max (integer num1,integer num2)
  • integer max
  • if num1 gt num2
  • max num1
  • else
  • max num2
  • endif
  • return max
  • END Cube

16
Functions in a program
  • Program Show Funcs
  • Declare integer num1,num2
  • Display Enter two numbers
  • Input num1,num2
  • Display "The max of " num1 " and " num2 "
    is " Max(num1,num2)
  • cube Cube(num1)
  • Display "The cube of " num1 " is "
    Cube(num1)
  • End ShowFuncs
  • //
  • //this function returns the cube of x
  • Integer  FUNCTION Cube (integer x)
  • return x x x
  • END Cube
  • //
  •  //this function returns the maximum of 2
    numbers
  • Integer  FUNCTION Max (integer num1,integer num2)
  • integer max
  • if num1 gt num2
  • max num1

17
When to use Functions versus Procedure
  • When returning more than one value - use
    Procedure
  • when I/O is required - use Procedure
  • returning one Boolean value - Function
  • returning one value to be used immediately in an
    expression - Function
  • when in doubt - use Procedure

18
arguments or parameters
  • In subprogram definition
  • Procedure Name(type arg1, type arg2,type argn)
    // Formal arguments
  • In call to subprogram
  • Call Name(arg1,arg2,argn) //Actual arguments
  • Arguments must match in number, order and data
    type but not name
  • Can be 0,1, or many agruments

19
Formal and Actual Arguments
  • Formal Argument
  • In the definition
  • Procedure PrintLines(integer lines)
  • Actual Argument
  • In the call
  • PrintLines(2) // could be constant
  • PrintLines(num) //could be variable
  • PrintLines(num2) //could be Expression
  • Note actual parameter and formal arguments may
    have different names

20
Multiple parameters
  • matched by position
  • each param must be declared
  • Procedure PrintLines(int numLines,char
    whichChar)
  • PrintLines(3,)

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.

22
(No Transcript)
23
  • Process_three_chars
  • Declare character char_1,char_2,char_3
  • Display Enter three characters
  • Get char_1,char_2,char_3
  • DOWHILE NOT (char_1 X AND char_2 X
    AND char_3 X)
  • IF char_1 gt char_2 THEN
  • temp char_1
  • char_1 char_2
  • char_2 temp
  • ENDIF
  • IF char_2 gt char_3 THEN
  • temp char_2
  • char_2 char_3
  • char_3 temp
  • ENDIF
  • IF char_1 gt char_2 THEN
  • temp char_1
  • char_1 char_2
  • char_2 temp

24
  • ProcessThreeCharacters
  • Declare character char_1,char_2,char_3
  • Display Enter three characters
  • Get char_1,char_2,char_3
  • DOWHILE NOT (char_1 X AND char_2 X
    AND char_3 X)
  • Call Sort_three_characters(char_1,char_2
    ,char_3)
  • Display char_1, char_2, char_3
  • Display Enter three characters
  • Get char_1,char_2,char_3
  • ENDDO
  • END

25
  • Procedure SortThreeCharacters(character c1,
    character c2,character c3)
  • Declare character temp
  • IF c1 gt c2 THEN
  • temp c1
  • c1 c2
  • c2 temp
  • ENDIF
  • IF c2 gt c3 THEN
  • temp c2
  • c2 c3
  • c3 temp
  • ENDIF
  • IF c1 gt c2 THEN
  • temp c1
  • c1 c2
  • c2 temp
  • ENDIF
  • END SortThreeCharacters

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

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

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

29
Value Parameters
  • Procedure PrintLines(int numLines)
  • numLines is formal parameter
  • 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
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
  • Program SwapNums
  • integer x,y
  • x 5
  • y 10
  • Display "Originally x " x " and y " y
  • Swap(x,y)
  • Display "Now x " x " and y " y
  • End SwapNums
  • Procedure Swap(integer u,integer v)
  • integer temp
  • temp u
  • u v
  • v temp
  • END Swap
  • Originally x 5 and y 10
  • Now x 5 and y 10

32
  • Program SwapNums
  • integer x,y
  • x 5
  • y 10
  • Display "Originally x " x " and y " y
  • Swap(x,y)
  • Display "Now x " x " and y " y
  • End SwapNums
  • Procedure Swap(integer u,integer v)
  • integer temp
  • temp u
  • u v
  • v temp
  • END Swap
  • Originally x 5 and y 10
  • Now x 10 and y 5

33
  • /// This program outputs an appropriate activity
    for a given temp.
  • /include ltiostream.hgt
  • 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 ltlt "Enter the outside temperature" ltlt
    endl
  • cin gtgt temp
  • cout ltlt "The current temperature is " ltlt temp
    ltlt endl

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

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

36
global variables
  • declared outside all functions
  • Declare integer gamma
  • Program main
  • .
  • END main
  • void SomeFunc()
  • .
  • END SomeFunc
  • can be accessed from main or SomeFunc

37
  • PROGRAM ShowScope
  • Declare real X1
  • Procedure Subprog1
  • Declare real X2
  • ..
  • END Subprog1
  • END ShowScope
  • X1 - global
  • X2 - local to Subprog1

PROGRAM ShowScope X1
Subprog1 X2
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

39
Name precedence
  • PROGRAM Demo
  • Declare integer age
  • PROCEDURE Subprog
  • Declare integer age
  • age 20

PROGRAM DEMO age
PROCEDURE Subprog age
40
  • include ltiostream.hgt
  • const int a 17 // global constant
  • int b // global variable
  • int c // global variable
  • Procedure Stuff
  • b 4
  • c 6
  • SomeFunc(42.8)
  • return 0
  • END Stuff
  • Procedure SomeFunc(float c) // prevents access
    to global c
  • float b // local var - prevents
    access to global b
  • b 2.3
  • Display "a " a
  • Display " b " b

output a 17 b 2.3 c 42.8
41
  • PROGRAM BLOCKSTRUCTURE
  • Declare real A0,B0,C0
  •  
  • PROCEDURE BLOCK1
  • Declare real A1,B1,C1
  • FUNCTION BLOCK11 REAL
  • Declare real A11,B11,C11
  • BEGIN
  • .
  • END of block 11
  • BEGIN
  • .
  • END of block 1
  •  
  • PROCEDURE BLOCK2
  • Declare real A2,B2,C2
  • BEGIN
  • .
  • END of block 2
  • The identifiers A0,B0, and C0 are declared in the
    outer block of the program called BLOCKSTRUCTURE.
    They are therefore accessible at all levels of
    the program and are termed global. The variables
    A1, B1, and C1 are declared in BLOCK1, so they
    are available only within that block. They would
    be available in BLOCK1, but could not be
    referenced from BLOCK2 as BLOCK1 and BLOCK2 are
    disjoint. They could also not be referenced from
    the statements of the program outer block. They
    could however, be accessed in BLOCK11 as the
    inner block implicitly inherits variables from
    the surrounding block.
  • The variables A11, B11, and C11 can be referenced
    by statements within BLOCK11 but no others. An
    identifier declared within a block is said to be
    local to that block.

42
NESTING FUNCTION CALLS
  • function definitions cannot be nested in C but
    function calls can
  • Procedure Misc
  • ...
  • DoCalcs()
  • END Misc
  • Procedure DoCalcs()
  • DoAvg(x,y,z)
  • DoMedian(x,y,z)
  • END DoCalcs
  • float Function DoAvg(int a,int b,int c)
  • integer i
  • return((a b c)/3)
  • END DoAvg
  • ....

43
  • PROGRAM Sample
  • Declare integer num1,num2
  • num1 10
  • num2 7
  • Change(num1,num2)
  • Display(num1,num2)
  • END Sample
  • PROCEDURE Change ( integer x,integer y)
  • Declare integer num2
  • num2 x
  • y y num2
  • x y
  • End Change
  •  
  • What is the output of the program as written?
  • What if x is changed to a reference variable?
  • What if y is changed to pass by value?
Write a Comment
User Comments (0)
About PowerShow.com