Functions and Contracts
-
Writing Function Contracts
A contract states which inputs a function accepts, which outcomes it promises, and which state it may change.
-
Parameters and Arguments
Parameters name inputs in a definition. Arguments supply the values that reach those names at a particular call.
-
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.
-
Returning Several Related Values
A function can return one structured result containing several related values so callers receive a coherent observation.
-
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.
-
Pure Functions and Predictable Results
A pure function produces a result determined by its inputs and has no observable effects on the surrounding program.
-
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.
-
Function Preconditions
A precondition describes a property the caller is responsible for supplying when using a function under its stated contract.
-
Function Postconditions
A postcondition states what a caller can rely on after a function completes successfully for a valid input.
-
Returning a Value Versus Printing It
A return passes a value to the caller. A print call writes a representation to an output stream.
-
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.
-
Function Contracts for Retried Operations
A retry contract identifies whether repeating a call after a partial failure can duplicate an effect.
-
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.
-
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.
-
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.
-
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.
-
Tracing Nested Function Calls
A nested call has its own parameter bindings and returns a result to a specific pending operation in its caller.
-
Composing Two Functions
Composition applies one function to the result of another. The first result must satisfy the next function's input contract.
-
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.
-
Giving a Helper a Focused Responsibility
A focused helper performs work that can be described, tested, and reused with a small set of inputs.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
Changing a Function Contract Safely
A contract change can affect callers even when the function name and return type stay the same.
-
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.
-
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.
-
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.