📚

Modular Programs and Logic

May 23, 2024

Lecture Notes: Modular Programs and Logic

Overview

  • Topic: Structure of modular programs and mainline logic
  • Focus: Program modularization, naming conventions, flowchart symbols, and modular cohesion

Components of a Modular Program

  1. Module Header
    • Includes the identifier and important identifying information
  2. Module Body
    • Contains all the statements
  3. Module Return Statement
    • Marks the end of the module
    • Identifies the return point to the calling program/module
    • Essential even if some programming languages imply a return

Naming Modules

  • Similar to naming variables
  • Followed by parentheses (common in Java, C++, C#)
  • Good practice: use verbs (e.g., get, calculate, display)

Calling Modules

  • Main program calls the module name
  • Flowchart Symbol: Rectangle with a bar on top
    • Place the module name inside the rectangle
    • Example: sayName()
  • Separate modules with their own symbols (diamonds for sentinel symbols)

Example: Simple Program

  • Inputs: Name and Balance
  • Outputs: Address info, Customer info, and Balance
  • Process:
    • Prompt for name and balance
    • Output the following sequence
      1. ABC Manufacturing
      2. 47 Park Lane
      3. Umrah West and Zip code
      4. Customer info: name and balance

Modularization Example

  • Take out statements from the main program
  • Encapsulate statements within a module named displayAddressInfo
  • Encapsulation: Think of it as a capsule containing data
    • Reusable in other programs
  • Leads to greater functional cohesion
    • Logical grouping of related statements
  • Example: Output address info in a separate module

Functional Cohesion

  • No set rules for module creation
  • Select statements contributing to the same job
  • Read further on cohesion for deeper understanding (future chapter)

Named Constants and Local Variables

  • Declare constants at the beginning of the module
  • Local variables scope limited to the module
    • Example: line1, line2, line3 as string constants

Global Variables and Constants

  • Declared at the program level
  • Visible and usable in all modules called by the program
  • Avoid global variables to minimize errors

Typical Procedural Program Structure

  1. Declarations
    • Global variables and constants
  2. Housekeeping Tasks
    • Initialization tasks
  3. Detailed Loop Tasks
    • Core logic that repeats
  4. End of Job Tasks
    • Cleanup and final output
  • Analogy:
    • Housekeeping: Turn on lights, start machines
    • Detailed Loop: Produce chocolate bars (repeat)
    • End of Job: Turn off lights, stop machines, output production count

Conclusion

  • Join next session on creating hierarchy charts

End of notes.