# Functions and modular decomposition

Physics I · Computer Science · https://tryals.app/learn/physics-i/functions-and-modular-decomposition

A **function** encapsulates a reusable block of logic, taking **parameters**, computing, and **returning** a result.

```python
def kinetic_energy(mass, velocity):
    return 0.5 * mass * velocity**2
```

- **Reusability:** Write once, call for any data point.
- **Readability:** Named calls beat raw math.
- **Testability:** Verify independently.

**Scope:** Variables defined inside are **local** and do not exist outside.

```python
def f(x):
    result = x ** 2
    return result
# print(result) -> NameError!
```

**Modular decomposition** breaks complex problems into smaller functions solving single sub-tasks.

## Purity and Pitfalls

| Function Type | Behavior | Testing ||
| --- | --- | --- |
| **Pure** | Depends only on inputs, no side effects | Easy to test |
| **Impure** | Modifies external state or relies on globals | Harder to test |

**DRY Principle:** Don't Repeat Yourself. If you copy-paste code, extract it into a function to reduce bugs and ease maintenance.

> **Common Pitfall:** A function that **prints** versus one that **returns**. Printing is for humans to read, but only returning lets code compose functions together. Always return values for program use.

## Practice questions

3 of this lesson's 14 practice questions, with answers. The full set is in the app.

### 1. A 300-line script computes a rocket trajectory in one giant block. It works. Why would a good engineer still break it into functions?

A. Functions reduce the program’s memory footprint
B. Each piece can be understood, tested and fixed alone
C. Shorter files run faster on modern CPUs
D. The compiler requires functions beyond 100 lines

**Answer:** B. Each piece can be understood, tested and fixed alone

**Why:** Functions are the units of understanding, testing and reuse. The machine is indifferent, but the next human (usually you, three weeks later) can grasp `compute_drag()` in isolation. Managing complexity, not speed, is the core skill of programming.

Page: https://tryals.app/practice/physics-i/functions-and-modular-decomposition/a-300-line-script-computes-a-rocket-trajectory-in-one-giant-block-it

### 2. Order the steps of a function call.

**Answer:**

1. Caller evaluates arguments
2. Arguments bound to parameters
3. Function body executes
4. Return value sent back to caller

**Why:** Execution flows from argument evaluation through to the return.

Page: https://tryals.app/practice/physics-i/functions-and-modular-decomposition/order-the-steps-of-a-function-call

### 3. Order the execution when Python runs: `result = round(abs(-2.7))`

**Answer:**

1. Evaluate the literal −2.7
2. Call abs, producing 2.7
3. Call round, producing 3
4. Bind the name result to 3

**Why:** Composition runs inside-out: innermost value, inner call, outer call, then the assignment. Reading nested code as a pipeline of values is the fastest way to trace what any expression does.

Page: https://tryals.app/practice/physics-i/functions-and-modular-decomposition/order-the-execution-when-python-runs-result-round-abs-2-7
