Functions
A function is a named block of code, defined with def, that takes inputs as parameters and sends a result back with return.
Last updated: 30 Sep, 2026 · Python 3.14
The if chain in if, elif and else sorts one ticket. To sort another, you would copy it. A function gives the code a name so it can run again on different input.
print(). In a .py file and in the editor here, wrap it in print(), as the run below does.def even_odd(num):
if num%2==0:
print("The number is even")
else:
print("The num is odd")
even_odd(24)
def hello_world():
print("Hello Welcome")
val = hello_world()
print(val)
def hello_world():
return "Hello Welcome"
val = hello_world()
print(val)
def add_number(num1, num2):
return num1 + num2
val = add_number(24, 48)
print(val)The number is even Hello Welcome None Hello Welcome 72
The first hello_world printed its message, so val was None. The second returned it, so val held the text. The ticket version below makes the same choice: categorise returns its answer.
Syntax:
def name(parameter):
...
return resultDefining categorise
def categorise(text):
text = text.lower()
if "charged" in text or "refund" in text:
return "billing"
if "parcel" in text or "arrived" in text:
return "shipping"
return "other"
print(categorise("I was charged twice for one order"))
print(categorise("My parcel has not arrived"))
print(categorise("How do I change my password?"))billing shipping other
def starts a function. categorise is its name and text is its parameter: the name the input gets inside the function. The indented lines are what runs each time it is called.
return sends a value back to whoever called the function, and ends the function there. That is why the second if does not need an elif: once a return has run, nothing below it does.
Printing instead of returning
A function that prints its answer looks right until you try to use the answer.
def categorise_and_print(text):
if "charged" in text:
print("billing")
result = categorise_and_print("I was charged twice")
print("result is", result)billing result is None
billing appears, but result is None. A function without return gives back None, so the program cannot use what was shown. Print for people; return for code.
Calling one function from another
def label(ticket):
category = categorise(ticket["text"])
return f"#{ticket['id']} {category}"
print(label({"id": 3, "text": "Can I get a refund for the blue mug?"}))#3 billing
Once categorise exists, any later code can call it. Small functions that each do one thing are easier to test, and pytest tests this exact one.
print vs return
print(x) | return x | |
|---|---|---|
| Sends x to | The screen | The code that called the function |
| The call gives back | None | x |
| Use it for | People reading the output | Code that uses the result |
Where functions show up in AI code
- A tool an agent can call is a plain Python function like
categorise. - Wrapping a model call once, so the rest of the program calls
ask_model(text).
categorise without brackets does not run the function; it names it. Writing result = categorise stores the function itself, as Functions as values shows.Related
- Previous: List comprehensions
- Next: Function arguments
- Reference: Defining Functions in the Python tutorial
- Add a check for
"password"that returns"account". - Call
categorise()with nothing in the brackets and read the error. - Loop over a list of three ticket texts and print
categoriseof each.
You understood something today that you didn't yesterday.