4. Functions#
Functions let you name a reusable block of code and call it whenever you need that behavior.
Functions reduce repetition by giving a name to a reusable process
Parameters let a function work with different inputs
Return values let a function produce a result for later code to use
Scope controls where names are available
Recursion solves a problem by reducing it to smaller versions of itself
Chapter Video
This short overview introduces Python functions and the basic idea of defining reusable code with def.
Python Tutorial for Beginners 8: Functions (21:47) by Corey Schafer
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Define and call custom functions with
defUse parameters, arguments, defaults,
*args, and**kwargsUse
returnstatements to send results back to the callerExplain local and global scope, function composition, pure functions and side effects, docstrings, and basic lambda expressions
Write closures with nested functions, enclosing scope, and
nonlocalTrace recursive function calls using base cases and recursive cases
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Function |
A named, reusable block of code that performs a task. |
2 |
Function definition |
Code that creates a function using |
3 |
Function call |
An expression that runs a function by using its name followed by parentheses. |
4 |
Function body |
The indented block of statements that runs when a function is called. |
5 |
Parameter |
A variable name listed in a function definition. |
6 |
Argument |
A value passed into a function call. |
7 |
Default parameter |
A parameter that has a fallback value when the caller does not provide an argument. |
8 |
|
Syntax that collects extra positional arguments into a tuple. |
9 |
|
Syntax that collects extra keyword arguments into a dictionary. |
10 |
Return value |
The result a function sends back to the caller. |
11 |
Return statement |
A statement that exits a function and optionally sends a value back. |
12 |
Type annotation |
A note in code that records the expected type of a parameter or return value. |
13 |
Scope |
The region of a program where a name can be used. |
14 |
Local variable |
A variable created inside a function and available only within that function call. |
15 |
Function composition |
Building a result by using one function’s output as another function’s input. |
16 |
Docstring |
A string that documents what a function does. |
17 |
Lambda function |
A small anonymous function written as a single expression. |
18 |
Recursion |
A pattern where a function calls itself. |
19 |
Stack diagram |
A diagram that shows active function calls and their local variables. |
20 |
Infinite recursion |
Recursion that never reaches a stopping condition. |
21 |
Pure function |
A function whose result depends only on its inputs and that does not change outside state. |
22 |
Side effect |
A change or interaction outside a function’s return value, such as printing or mutating a list. |
23 |
Base case |
The branch of a recursive function that stops the recursion. |
24 |
Recursive case |
The branch of a recursive function that calls the function again on a smaller or simpler input. |
25 |
Memoization |
Storing previous results so repeated function calls can reuse them. |
26 |
Nested function |
A function defined inside another function. |
27 |
Enclosing scope |
The local scope of an outer function, which a nested function can read. |
28 |
Closure |
A function that remembers values from the scope where it was created, even after that scope has finished. |
29 |
Function factory |
A function that builds and returns new, configured functions. |
30 |
|
A keyword that lets a nested function assign to a variable in its enclosing scope. |
31 |
Global variable |
A variable defined at the top level of a program, outside any function, that code throughout the module can read. |
32 |
|
A keyword that lets a function assign to a global variable instead of creating a new local one. |
33 |
Keyword argument |
An argument passed by parameter name, such as |
34 |
Boolean function |
A function that returns |
35 |
Dead code |
Code that can never run, such as a statement that comes after a |
36 |
Stack frame |
The record of one active function call and its local variables; a stack diagram shows one frame per call. |