# Functions and Modularity

Mathematics I · Programming Elements · https://tryals.app/learn/mathematics-i/functions-and-modularity

## Reusable Blocks

A **function** packages logic under a name so it can be **reused** instead of rewritten. This is **modularity**: building programs from small, independent parts following the **DRY** principle ('Don't Repeat Yourself').

| Element | Role |
| --- | --- |
| **Parameters** | Named inputs received when called |
| **Body** | The algorithmic steps performed |
| **Return value** | The output handed back to caller |

Calling `f(3, 4)` passes arguments to parameters, executing the logic once and invoking it many times with distinct inputs.

## Scope & Encapsulation

**Scope** governs where names are visible. A **local variable** defined inside a function exists only there and vanishes upon return. 

This **encapsulation** hides internal details behind a clean **interface**. Two functions can safely use a variable named `i` without interference.

| Concept | Meaning |
| --- | --- |
| **Abstraction** | Treating a function as a reliable black box |
| **Interface** | The known inputs and outputs |

> **Common pitfall:** Expecting a **local variable** to be visible **outside** its function. Functions communicate strictly through parameters and return values.

## Practice questions

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

### 1. A function that reads no globals and changes nothing outside itself is called **pure**. Why do such functions make a program easier to reason about?

A. Because they can be written without loops
B. Because they execute faster than functions with side effects
C. Because they use less memory during execution
D. Because their behaviour depends only on their arguments

**Answer:** D. Because their behaviour depends only on their arguments

**Why:** Everything a pure function does is in its arguments, so you can reason about it without holding the rest of the program in your head, and it can be memoised, parallelised or reordered for free. Concentrating side effects at the edges is how large systems stay tractable, whatever the paradigm.

Page: https://tryals.app/practice/mathematics-i/functions-and-modularity/a-function-that-reads-no-globals-and-changes-nothing-outside-itself

### 2. Sort each change to a function by whether existing callers keep working untouched.

**Answer:**

- Callers unaffected: Rename a local variable inside the body, Replace the body with a faster method giving the same results, Add a comment explaining the method
- Breaks callers: Add a required second parameter, Change the function to return nothing

**Why:** Internal names, internal methods and comments are invisible to callers; changing the parameters or the return value changes the interface itself and every call site must follow. Knowing which is which is what makes modular code safe to improve.

Page: https://tryals.app/practice/mathematics-i/functions-and-modularity/sort-each-change-to-a-function-by-whether-existing-callers-keep

### 3. If two different functions each define a local variable named count, changing one of them changes the other.

**Answer:** False

**Why:** False. Each function's count is a separate variable that exists only during that function's execution, so they cannot collide. This independence is what allows functions written by different people to be combined safely.

Page: https://tryals.app/practice/mathematics-i/functions-and-modularity/if-two-different-functions-each-define-a-local-variable-named-count

### 4. Order what happens when the call f(3, 4) is evaluated.

**Answer:**

1. The caller evaluates the arguments 3 and 4
2. The arguments are bound to the function's parameters
3. The body runs, creating local variables as it goes
4. The function returns its result to the caller
5. The function's local variables cease to exist

**Why:** Arguments are evaluated, bound to parameters, the body runs, a value comes back, and the locals vanish. That last step is encapsulation in action: the internals are gone, so only the returned value can influence the caller.

Page: https://tryals.app/practice/mathematics-i/functions-and-modularity/order-what-happens-when-the-call-f-3-4-is-evaluated

### 5. Extracting repeated code into a function usually makes a program longer, not shorter, once the definition and calls are counted. Why is that a good trade?

A. Less memory use, since the code exists only once at runtime.
B. One place where the logic lives, so a fix is made once.
C. A faster program, since a function call is cheaper than executing the code inline.
D. A guarantee that the extracted code is correct, since functions are checked separately by the compiler.

**Answer:** B. One place where the logic lives, so a fix is made once.

**Why:** Duplicated logic means duplicated bugs, and the failure mode is fixing four of five copies, worse than not noticing, because the bug now appears intermittently. The name does real work too: `is_valid_email(s)` tells a reader what the code accomplishes without their reading how. A call costs a little, so modularity is bought at a small runtime price worth paying.

Page: https://tryals.app/practice/mathematics-i/functions-and-modularity/extracting-repeated-code-into-a-function-usually-makes-a-program
