# Functions and Parameter Passing

Computer Science I · Programming I · https://tryals.app/learn/computer-science-i/functions-and-parameter-passing

## Packaging Logic

A **function** is a named, reusable block of code that performs a specific task. Defining a function once and **calling** it many times avoids repetition.

A function takes **parameters** (named inputs in the definition) and receives **arguments** (actual values passed during the call).

```
def square(n):
    return n * n

y = square(5) # y becomes 25
```

The **return** statement hands a result back to the caller and ends the function execution. Functions enable **abstraction**, letting you use code without knowing its internal implementation.

## Scope and Pitfalls

**Scope** governs where variables are visible. **Local** variables are created inside functions and exist only during that call. **Global** variables are declared outside all functions and are visible everywhere.

| Term | Definition |
| --- | --- |
| **Parameter** | Name listed in the function definition |
| **Argument** | Actual value passed during a function call |

> **Common pitfall:** Confusing parameters with arguments. Also, trying to access a **local** variable from outside its function will cause an error because it does not exist there.

## Practice questions

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

### 1. Whether a function can modify its caller's argument depends on whether the argument is mutable, not on how it was passed.

**Answer:** True

**Why:** True. Python always passes a reference to the object; what differs is whether the object can be mutated. `lst.append(x)` changes the caller's list, while `n += 1` rebinds a local name because integers are immutable. Rebinding the parameter never affects the caller, whatever the type.

Page: https://tryals.app/practice/computer-science-i/functions-and-parameter-passing/whether-a-function-can-modify-its-callers-argument-depends-on

### 2. For def scale(factor): return factor * 10, later called as scale(60), sort each item by what it is.

**Answer:**

- Parameter: factor, named in the definition
- Argument: 60, supplied in the call
- Return value: 600, handed back to the caller

**Why:** factor is the parameter (a name in the definition), 60 is the argument (the actual value passed), and 600 = 60 * 10 is the return value. Confusing parameters with arguments is the pitfall named in this lesson: the definition declares names, the call supplies values.

Page: https://tryals.app/practice/computer-science-i/functions-and-parameter-passing/for-def-scale-factor-return-factor-10-later-called-as-scale-60

### 3. Match each term to its meaning.

**Answer:**

- Parameter → A name listed in the function's definition
- Argument → The actual value passed in a call
- Return → Ends the function and hands a result back
- Local variable → Exists only during the call, invisible outside

**Why:** Parameters are the definition's names, arguments are the call's values, return both ends the function and yields a result, and a local variable lives only for the duration of the call. Expecting to read a local from outside the function is the second pitfall this lesson names.

Page: https://tryals.app/practice/computer-science-i/functions-and-parameter-passing/match-each-term-to-its-meaning

### 4. A function with a default argument `def f(x, items=[])` behaves strangely: the list keeps its contents between calls. Why?

A. Because the default is evaluated once at definition, not on each call
B. Because lists cannot be used as default arguments and are silently converted
C. Because the function retains its local variables between invocations
D. Because the caller's list is being modified through the reference

**Answer:** A. Because the default is evaluated once at definition, not on each call

**Why:** One list is created at definition time and shared by every default-argument call, so appends accumulate across calls. The idiom is `items=None` with a check inside, and this trap is why mutable defaults are near the top of every Python gotchas list.

Page: https://tryals.app/practice/computer-science-i/functions-and-parameter-passing/a-function-with-a-default-argument-def-f-x-items-behaves

### 5. Order what happens when the call y = sq(2 + 3) runs, where def sq(n): return n * n.

**Answer:**

1. Evaluate the argument expression 2 + 3, giving 5
2. Bind the parameter n to the value 5
3. Run the body: compute n * n, giving 25
4. Return 25 to the caller
5. Store the returned 25 into y

**Why:** The argument 2 + 3 is computed to 5 first, then bound to n, the body runs, 25 is returned, and only then is it stored into y. The parameter n takes the argument's value at binding time, it is not linked to the expression 2 + 3 afterwards.

Page: https://tryals.app/practice/computer-science-i/functions-and-parameter-passing/order-what-happens-when-the-call-y-sq-2-3-runs-where-def-sq-n

### 6. Select every statement about scope that is true.

A. A variable declared outside all functions is global
B. A local variable can be accessed after the function returns
C. A variable created inside a function is local to that call
D. Local scope lets two functions each use a variable named i without conflict
E. Relying heavily on globals makes code easier to reason about

**Answer:** A. A variable declared outside all functions is global; C. A variable created inside a function is local to that call; D. Local scope lets two functions each use a variable named i without conflict

**Why:** Locals are confined to their call and vanish on return, so they cannot be read afterward; a name outside all functions is global; and local scope is exactly what lets two functions reuse the name i safely. The lesson warns that leaning on globals makes code harder to reason about, so the fifth option is false.

Page: https://tryals.app/practice/computer-science-i/functions-and-parameter-passing/select-every-statement-about-scope-that-is-true
