Skip to main content
← Python Study Guide

Unit 2 · Foundational

Functions and Program Design

Decomposition, and the scope rules that make it safe. This is the unit that decides whether later programs stay readable as they grow.

What a strong answer looks like

A strong Unit 2 answer states what a function takes, what it returns, and what it leaves unchanged, before any of its body.

Topics in this unit

1

Parameters and Arguments

Know

Python passes references to objects. Rebinding a parameter inside a function does not affect the caller, but mutating a mutable argument does.

Apply

Decide for each parameter whether the function may modify it, and document that.

Watch out

Mutating a list argument unintentionally, which changes the caller data invisibly.

Study move

Write one function that fails to change a number and one that changes a list, and explain both.

2

Return Values

Know

A function without an explicit return yields the null object. Returning is different from printing, and only one of them is usable by the caller.

Apply

Return results and let the caller decide whether to display them.

Watch out

Printing inside a function then trying to use the result, which silently gives nothing.

Study move

Convert a printing function into a returning one and update its caller.

3

Scope

Know

Names assigned inside a function are local unless declared otherwise. Reading an outer name works; assigning to it creates a new local one.

Apply

Pass values in and return them out rather than reaching for globals.

Watch out

Assigning to a name that also exists outside, which creates a local and leaves the outer one untouched.

Study move

Write a function that appears to modify a global counter but does not, then fix it properly by returning.

4

Default and Keyword Arguments

Know

Defaults are evaluated once when the function is defined, so a mutable default is shared across every call.

Apply

Use an immutable sentinel as the default and create the mutable value inside the body.

Watch out

A mutable default argument accumulating values between calls, which is one of the most confusing bugs in the language.

Study move

Write a function with a mutable default, call it three times, and explain the growing result.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • Stating a function contract before writing it
  • Returning rather than printing
  • Avoiding mutable default arguments

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • Lambdas. Introduced here in some sections and with comprehensions in others.
  • Docstrings. Required in some courses as part of the contract.

Mastery checklist

  • Say whether a function can modify each of its arguments.
  • Explain the difference between returning and printing.
  • Predict what a name assigned inside a function affects.
  • Explain why a mutable default is dangerous.

Check yourself

  • Why can a function change a list argument but not a number argument?
  • What does a function return when it has no return statement?
  • When is a default argument evaluated?

Modeling drill

Write a function that takes a list of scores and returns the top three without modifying the original list, then prove the original is unchanged.

ParameterArgumentReturnLocal scopeGlobal scopeDefault argumentMutable default