OIM3640 - Problem Solving and Software Design

contain

Functions: Decomposition & Abstraction

How do we write code?

  • So far, we've learned:
    • Basic programming concepts
    • Writing small programs in separate files
    • Code is made up of step-by-step instructions
  • The challenge:
    • This works for small tasks, but quickly becomes messy for bigger projects
    • Hard to keep track of details
    • Not always clear which code should go where

Writing Better Programs

  • Writing more code doesn't mean writing better code
  • A good programmer is judged by what the program can actually do
  • Functions help us:
    • Decomposition → break big problems into smaller, manageable parts
    • Abstraction → hide the messy details, focus on the inputs and outputs

Example - Drone

  • Think of a drone as a black box:

    • We don’t know how it works inside
    • We only see the interface: input (commands) → output (actions)
    • We control it with a remote or an app
    • It flies and takes videos based on our commands
  • ABSTRACTION: To use a drone, you don’t need to understand its inner mechanics

Example – Drone Light Show

  • In a drone light show, thousands of drones work together:

    • Each drone gets its own task
    • Each one follows a programmed path
    • Together, they form amazing patterns in the sky
  • DECOMPOSITION: A big show becomes possible by splitting it into many smaller drone tasks

From Drones to Code

  • DECOMPOSITION

    • Break a big problem into smaller, self-contained pieces
    • Each part can work on its own
  • ABSTRACTION

    • Hide the inner details of how something works
    • Focus on the inputs, outputs, and the result

Decomposition

  • With drones, we split the work among many units

  • In programming, we split code into modules or functions:

    • Each part is self-contained
    • Easier to manage and reuse
    • Keeps the program organized and clear
  • Today: we’ll use functions for decomposition

  • Later: we’ll see how classes (OOP) extend this idea

Abstraction

  • With the drone, you don’t need to know how it’s built

  • In programming, a function is like a black box:

    • Inner details are hidden
    • We only care about input → output → result
  • Abstraction saves us from messy details

  • We describe it with function specs or docstrings

Functions

  • A function is a reusable block of code
    • It just sits there until you call (use) it
    • Think of it as a tool you can pick up when needed
  • Key parts of a function:
    • Name → how we call it
    • Parameters → inputs it can take
    • Docstring (optional but strongly recommended) → explanation of what it does
    • Body → the code it runs
    • Return value → the result it gives back

Civilization advances by extending the number of operations we can perform without thinking about them.

- Alfred North Whitehead (British mathematician and philosopher, 1861–1947)