OIM3640 - Problem Solving and Software Design

contain

Functions

How do we write code?

  • What we have learned so far:
    • Basic understanding of programming concepts.
    • Writing separate files for different tasks.
    • Each file is some piece of code.
    • Code is composed of individual instructions.
  • Problems with this approach:
    • It may work for smaller problems, but can get messy for complex projects.
    • It is hard to manage details and keep track of information.
    • How do you know the right information goes to the right part of code?

Achieving Good Programming

  • More code doesn't always mean better programming.
  • A good programmer is evaluated by the functionality they deliver.
  • Functions are essential tools for decomposition and abstraction.
    • Decomposition: breaking complex problems into smaller, manageable parts
    • Abstraction: hiding complexity and presenting only the essential details to the user

Example - Drone

  • A drone is a black box:
    • You don't know how it works.
    • You only know the interface: input/output.
    • You can operate it with a remote control or an app.
    • It somehow captures aerial footage and performs maneuvers based on the input commands.
  • ABSTRACTION: Understanding the mechanics the drone is not necessary to use and operate it.

Example - Drones

  • Coordinating multiple drones for a drone light show:
    • Each drone was assigned a specific task.
    • Each drone followed a programmed path and synchronized with others.
    • All drones worked together to create a complex and visually stunning display
  • DECOMPOSITION: Dividing the task among multiple drones enables for efficient creation of a synchronized light show.

Apply These Ideas to Programming

  • DECOMPOSITION
    • Break a complex problem into different self-contained pieces
    • Each component or module functions independently.
  • ABSTRACTION
    • Hide the details of a method's implementation
    • Focus on the result of the process, not the underlying details

Create Structure with DECOMPOSITION

  • In the previous example, we use multiple drones.
  • In programming, we divide code into modules.
    • They are self-contained
    • They help to break up the code into smaller, more manageable parts
    • They are designed to be reusable, making the code more efficient
    • They help keep the code organized and coherent
  • In this lecture, we use functions to achieve decomposition.
  • Later in the course, we will further explore decomposition using classes (in OOP).

Suppress Details with ABSTRACTION

  • In the previous example, you didn't need to know how to build a drone.
  • In programming, we view a piece of code as a black box
    • The internal details are hidden and not visible.
    • It is not necessary to know the internal details.
    • The focus is on the input/output and desired result.
    • Abstraction helps to hide tedious coding details.
  • We achieve abstraction with function specifications and/or docstrings which outline the purpose and usage of the code.

Functions

  • Functions are reusable chunks of code in a program.
  • They are not executed until they are called or invoked.
  • Key characteristics of functions:
    • A name to identify and call the function.
    • Parameters (0 or more) to provide input to the function.
    • A docstring (optional but strongly recommended) to describe the purpose and usage of the function
    • A body containing the code to be executed when the function is called.
    • A return type (optional, not mandated) indicating the type of value the function returns.

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

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