Functions and Contracts

  1. Writing Function Contracts

    A contract states which inputs a function accepts, which outcomes it promises, and which state it may change.

    Undergraduate
  2. Parameters and Arguments

    Parameters name inputs in a definition. Arguments supply the values that reach those names at a particular call.

    Undergraduate
  3. Using Keyword Arguments to Explain a Call

    A keyword argument supplies a value to a named parameter, making roles and optional settings more visible at the call site.

    Undergraduate
  4. Returning Several Related Values

    A function can return one structured result containing several related values so callers receive a coherent observation.

    Undergraduate
  5. Mutable Default Arguments in Python

    A default argument expression is evaluated when Python executes the function definition. A mutable default is shared across calls that omit the argument.

    Undergraduate
  6. Pure Functions and Predictable Results

    A pure function produces a result determined by its inputs and has no observable effects on the surrounding program.

    Undergraduate
  7. Functions That Modify Their Inputs

    Some functions update the objects supplied to them. Their contracts should say which objects change and which result the caller can use.

    Undergraduate
  8. Function Preconditions

    A precondition describes a property the caller is responsible for supplying when using a function under its stated contract.

    Undergraduate
  9. Function Postconditions

    A postcondition states what a caller can rely on after a function completes successfully for a valid input.

    Undergraduate
  10. Returning a Value Versus Printing It

    A return passes a value to the caller. A print call writes a representation to an output stream.

    Undergraduate
  11. Default Arguments and Domain Policy

    A default argument fills in an omitted parameter. The default should represent a documented choice that is appropriate for the function's typical use.

    Undergraduate
  12. Function Contracts for Retried Operations

    A retry contract identifies whether repeating a call after a partial failure can duplicate an effect.

    Undergraduate
  13. Passing a Dependency as an Argument

    An argument can supply an operation or resource a function depends on, making that dependency explicit and replaceable for testing.

    Undergraduate
  14. Passing a Function as a Callback

    A callback is an operation supplied to another procedure to perform a defined part of that procedure's work.

    Undergraduate
  15. Returning a Function with Captured Data

    A returned function can use values from the lexical environment in which it was created, including after the creating call finishes.

    Undergraduate
  16. Recursive Calls and Ordinary Repeated Calls

    Recursion happens when executing a function leads to another call to the same function, directly or through other calls.

    Undergraduate
  17. Tracing Nested Function Calls

    A nested call has its own parameter bindings and returns a result to a specific pending operation in its caller.

    Undergraduate
  18. Composing Two Functions

    Composition applies one function to the result of another. The first result must satisfy the next function's input contract.

    Undergraduate
  19. Returning an Error Result or Raising an Exception

    A routine can signal failure through a structured result or by raising an exception. The caller needs a clear rule for recognizing and handling that outcome.

    Undergraduate
  20. Giving a Helper a Focused Responsibility

    A focused helper performs work that can be described, tested, and reused with a small set of inputs.

    Undergraduate
  21. Documenting a Function with a Docstring

    A docstring should tell a caller what the operation means, which inputs are valid, and what result or failure to expect.

    Undergraduate
  22. Choosing a Scope for a Helper Function

    A helper's scope affects where it can be reused, which data it captures, and how independently it can be checked.

    Undergraduate
  23. Functions Accepting a Variable Number of Arguments

    A variadic signature can collect zero or more supplied positional values. Give the empty case a documented meaning.

    Undergraduate
  24. Unpacking Arguments at a Call Site

    Argument unpacking supplies elements of a sequence as positional arguments or mapping entries as named arguments where the language permits it.

    Undergraduate
  25. Designing a Function with Multiple Input Forms

    An interface accepting several forms needs an explicit decision about ambiguous values, conversion, and how effects depend on the chosen form.

    Undergraduate
  26. Public Interfaces and Internal Helpers

    A public function forms part of the promise made to callers. An internal helper can change with the implementation as long as that promise holds.

    Undergraduate
  27. Changing a Function Contract Safely

    A contract change can affect callers even when the function name and return type stay the same.

    Undergraduate
  28. Returning a Decision with Its Reason

    A structured decision can carry both the outcome and a reason a caller can use for display, logging, or a follow-up action.

    Undergraduate
  29. Who Owns a Function Input

    A caller and function can share an input object, transfer its use by convention, or choose an independent copy. Each choice has observable consequences.

    Undergraduate
  30. Testing Function Invariants Across Inputs

    An invariant is a property expected to hold on a class of calls. Testing it checks more than a single example result.

    Undergraduate