Language Syntax

Function definition — Python syntax

Create a function object and bind the name when the definition runs; defaults are evaluated then. The body normally executes on calls, and annotations do not enforce types. Decorators may replace the bound object.

Formal Syntax & Structure

python syntaxpython
def name(parameters) -> annotation:    suite

Working Example

Example Snippetpython
def total(price, quantity=2):    return price*quantityprint((total(3), total(3,4)))
Python Reference