Featureintro
Defining functions
Also called def statement, define a function, function definition, docstring, function body, def keyword.
The def statement makes a function object and binds it to a name. The body runs only when the function is called, and each call gets a new local symbol table.
- Language
- Python
Synopsis
def fib(n):
"""Return the Fibonacci numbers less than n."""
result = []
a, b = 0, 1
while a < n:
result.append(a)
a, b = b, a + b
return result
print(fib(100))
print(fib.__doc__)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Return the Fibonacci numbers less than n.
def gives the name fib to a new function. The string on the first line of the body is the docstring.
Semantics
r1. "The keyword def introduces a function definition. It must be followed by the function name and the parenthesized list of formal parameters. The statements that form the body of the function start at the next line, and must be indented."1
r2. "A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object".2 Example: runs-when-reached.
r3. "The function definition does not execute the function body; this gets executed only when the function is called."2 Example: body-later.
r4. "The first statement of the function body can optionally be a string literal; this string literal is the function’s documentation string, or docstring."1 Example: fib.
r5. "The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table".1 Example: local.
r6. Arguments "are passed using call by value (where the value is always an object reference, not the value of the object)."1 Example: object-reference.
r7. "Other names can also point to that same function object and can also be used to access the function".1 Example: other-name.
r8. "Functions are first-class objects. A “def” statement executed inside a function definition defines a local function that can be returned or passed around."2
Examples
def broken():
return undefined_name
print("defined")
defined
The body uses a name that does not exist, but Python does not run the body, so there is no error (rule r3).
fast = True
if fast:
def speed():
return "fast"
else:
def speed():
return "slow"
print(speed())
fast
def is a statement, so Python runs only the def that it reaches (rule r2).
total = 0
def add(n):
total = n
return total
print(add(5), total)
5 0
The assignment in add makes a local total. The global total does not change (rule r5).
def grow(items):
items.append(4)
items = [0]
numbers = [1, 2, 3]
grow(numbers)
print(numbers)
[1, 2, 3, 4]
append changes the list that the caller gave. The assignment items = [0] binds only the local name (rule r6).
def greet(name):
return "hello " + name
say = greet
print(say("ann"), say is greet, greet.__name__)
hello ann True greet
Pitfalls
A call before the def statement runs is a NameError, because the name is not bound yet (rule r2):
This code raises NameError, on purpose.
print(square(3))
def square(x):
return x * x
A function with no return statement gives None:
This code runs but gives a wrong answer on purpose.
def double(x):
x * 2
print(double(4))
None
The tutorial says: "even functions without a return statement do return a value, albeit a rather boring one. This value is called None".1 Write return x * 2.
Comparison
In Go, "A function declaration binds an identifier, the function name, to a function."3 In Go, "The scope of an identifier denoting a constant, type, variable, or function (but not method) declared at top level (outside any function) is the package block."3 So a Go function can call a function that is declared later in the file. A Go signature gives a type to each parameter, and "If the function's signature declares result parameters, the function body's statement list must end in a terminating statement."3 A Python def has no types that Python checks, and a function without return gives None (rule r1).
References
- The Python Tutorial, 4. More Control Flow Tools, 4.8. Defining Functions
- The Python Language Reference, Python 3.15, 8.7. Function definitions
- The Go Programming Language Specification, Function declarations; Declarations and scope
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag T729GH Edit this page History Report an error Markdown