Functions
def, return, docstrings, recursion, and optional type annotations. Run a cell to see its output beside the expected one.
def and return
Docstrings
A bare string as the first statement documents the function and compiles to nothing.
Recursion
A double holds every integer up to 2^53 exactly, so a counter can stop the recursion with equality; a comparison such as n < 1.5 also stops it when n is not a whole number.
Type annotations
Optional; the compiler checks them before the program runs.
A def with no return gives nothing
A def that ends without a return is called as a statement. Its value is nothing: the compiler refuses show f(x) or f(x) + 1.0 for it.
Early return
Call before definition
Top-level defs are collected before anything runs, so source order does not matter.
Lambdas pass as arguments
A def can take a function value and call it.
Parameters shadow globals
Several results
return a, b gives two values, and the caller unpacks them on a line of its own; a, b = b, a swaps.