Functions
A function is a named block of code that does one job. You write it once with def, then call it by name whenever you need the job done.
Functions: Name a job and reuse it.
Each term is defined where it first appears, in this order:
- Function: a named block of code that does one job.
- def: the keyword that stores a function under a name. A def line does not run the function.
- Call: a line such as
make_title("Pasta")that runs the function. - Parameter: a name in the def line that receives a value, such as
topic. - Argument: a value passed in a call, such as
"Pasta". - Default value: a value written in the def line, such as
prefix="Chat", that is used when a call leaves that argument out. - return: a line that hands a value back to the code that made the call.
- Docstring: a string used as the first statement in a function's body, which describes the function.
This function joins a prefix, a colon and a topic. The prefix default and the separator : are made up for this lesson.
def make_title(topic, prefix="Chat"):
return prefix + ": " + topicdef stores the function. A call runs it.
Python reads a def line and keeps the function under its name. The body, the indented lines, does not run then. When this block runs as a script file, it prints nothing.
def make_title(topic, prefix="Chat"):
return prefix + ": " + topicA call such as make_title("Pasta") runs the body. The result is handed back, which the return section explains. In this script the call does not show the result, so nothing appears on screen.

def stores the function. The call is the step that runs the body.Everyday analogy: a recipe card in a drawer. Writing the card does not cook anything. Cooking starts when someone follows it.
Parameters and arguments.
A parameter is a name in the def line. An argument is the value you pass in the call. Each argument fills a parameter. A parameter with a default can be left out of the call.
- In
def make_title(topic, prefix="Chat"),topicandprefixare parameters. - In
make_title("Pasta"),"Pasta"is an argument. It fillstopic. - An argument can also name its parameter, as in
make_title("Pasta", prefix="Note"). That call fillsprefixwith"Note".
A default fills the gap.
A parameter written as prefix="Chat" has a default value. If the call leaves that argument out, Python uses the default. If the call passes a value, the passed value is used for that call only.
make_title("Pasta") # prefix is "Chat"
make_title("Pasta", "Recipe") # prefix is "Recipe"
prefix out.Everyday analogy: a coffee order. A customer who says only "latte" gets the shop's usual size. Saying "large" changes the size for that order only. The shop and its sizes are invented here.
Practice: Read the default.
return hands a value back.
return sends a value out of the function to the code that called it. It also ends the function at that line. The caller can store the value in a variable.
def make_title(topic, prefix="Chat"):
return prefix + ": " + topic
x = make_title("Soup")
print(x) # Chat: Soup
return sends the value out to the caller, and ends that call.A function with no return line gives back None. That is Python's value for no result. The body still runs when the function is called.
def make_note(text):
note = "Note: " + text
x = make_note("Milk")
print(x) # NoneThe text built inside make_note is not handed back, so x holds None.
Practice: print or return?
A docstring says what the function does.
A docstring is a string used as the first statement in the function body, under the def line. Python keeps it with the function, and help() shows it.
A common layout has three parts: a one-line summary, an Args: list, and a Returns: line. This is one common style, not the only one.
def make_title(topic, prefix="Chat"):
"""Build a title from a prefix and a topic.
Args:
topic: The subject, such as "Pasta".
prefix: The word before the colon.
Defaults to "Chat".
Returns:
The title as one string,
such as "Chat: Pasta".
"""
return prefix + ": " + topic
help(make_title)Reading the docstring, part by part:
- Summary: the first line says what the function does. Keep it to one line.
- Args: one entry for each parameter, with its meaning. A default value goes here too.
- Returns: the value the caller receives. It does not describe text shown on screen.
Calling help(make_title) prints the signature, make_title(topic, prefix='Chat'), and then the docstring with its indentation removed.
Quick questions
- Does a docstring change what the function returns?
- No. It is a string that describes the function. The return line decides the result.
- Does a Returns entry describe what gets printed?
- No. It describes the value handed back to the caller. Text on screen comes from
print. - Is a docstring required?
- No. Python runs the function without one. It helps readers, and it is worth writing for functions other people will use. Keep it in step with the code, because
help()shows it as is.
Try the title builder.
The toy code holds the two functions from this lesson. Each button sets one call. The lab shows the text that is printed on screen and the value handed back to the caller as two separate results. Both def lines always run, because they store the functions.
Try it in this order:
- Press
make_title("Pasta"). Read the handed back value, then the shown value. - Press
make_title("Pasta", "Recipe"). Note which prefix changed. - Press
show_title("Pasta"). Look for the text under Shown on screen, andNoneunder Handed back. - Press
print(make_title("Pasta")). The same text now appears on screen.
Toy sandbox: a made-up Python snippet with the two functions from this lesson, not a live interpreter. Highlighted lines ran. Dimmed body lines were skipped. The def lines always run to store the functions.
def make_title(topic, prefix="Chat"): return prefix + ": " + topic def show_title(topic): print("Title: " + topic) make_title("Pasta")
Nothing"Chat: Pasta"Check your understanding.
Take this with you.
- def stores, a call runs: writing a def line does not run the body.
- Arguments and defaults: an argument fills a parameter. A default fills in when the call leaves it out.
- return or print:
returnhands a value back to the caller.printonly shows text. - Docstring: a string at the start of the body with a summary, Args and Returns.
help()shows it.