PythonDefining Functions

Defining Functions

A function is a named, reusable block of code that performs a specific task. Instead of copying and pasting the same logic every time you need it, you wrap it in a function, give it a name, and call that name whenever you need the behavior. Functions are one of the most important tools for writing clean, maintainable Python — they let you break a large problem into small, testable pieces.

The `def` Syntax

Functions are created with the def keyword, followed by a name, a pair of parentheses (which may contain parameters), and a colon. The body of the function is indented.

Python
def greet(name):
    message = f"Hello, {name}!"
    print(message)


# Calling the function
greet("Ada")
greet("Grace")
Hello, Ada!
Hello, Grace!

Here, greet is the function name and name is a parameter — a placeholder for a value the function expects to receive. def defines the function but does not run it; the code inside only executes when the function is called using greet(...).

Parameters vs Arguments

These two words are often used interchangeably, but they mean slightly different things:

  • Parameter: the name listed in the function definition (e.g. name in def greet(name):).

  • Argument: the actual value passed in when the function is called (e.g. "Ada" in greet("Ada")).

In practice, people say "pass an argument" and "define a parameter" — the parameter is the seat, the argument is who sits in it.

The `return` Statement

print() displays something to the console, but it does not give the value back to the rest of your program. To hand a result back to the caller, use return.

Python
def add(a, b):
    return a + b


result = add(3, 5)
print(result)       # 8
print(add(10, 20))  # 30, used directly
8
30

As soon as Python executes a return statement, the function stops running immediately — any code after it in the same function is skipped.

Python
def check_sign(n):
    if n > 0:
        return "positive"
    if n < 0:
        return "negative"
    return "zero"
    print("this line never runs")  # unreachable


print(check_sign(-4))
negative
Implicit `None` Return

If a function does not hit a return statement — or uses a bare return with no value — it automatically returns None. This is easy to forget and a common source of bugs.

Python
def log_message(msg):
    print(f"[LOG] {msg}")
    # no return statement here


output = log_message("Server started")
print(output)  # None
[LOG] Server started
None
Note
Every function in Python returns *something*. If you never write `return`, Python quietly returns `None` on your behalf.
Docstrings

A docstring is a string literal placed as the very first statement inside a function body. It documents what the function does and is accessible at runtime via function.__doc__ or help(function).

Python
def area_of_circle(radius):
    """Return the area of a circle given its radius."""
    pi = 3.14159
    return pi * radius ** 2


print(area_of_circle(2))
print(area_of_circle.__doc__)
12.56636
Return the area of a circle given its radius.
Tip
Use triple-quoted strings (`"""..."""`) for docstrings even on a single line — it is the universal Python convention and lets tools like Sphinx and IDEs pick them up consistently.
Functions Are First-Class Objects

In Python, functions are objects just like integers, strings, or lists. That means you can assign a function to a variable, store functions in a list or dictionary, and pass them as arguments to other functions.

Python
def shout(text):
    return text.upper() + "!"


# Assign the function itself (no parentheses) to a new name
yell = shout
print(yell("hello"))       # HELLO!

# Store functions in a data structure
operations = {"shout": shout}
print(operations["shout"]("wow"))  # WOW!
HELLO!
WOW!

Notice yell = shout has no parentheses — that copies a reference to the function object, not the result of calling it. This first-class nature is what makes higher-order concepts like lambdas, closures, and decorators possible in Python, which you'll meet later in this section.

Quick Reference

Concept

Example

Notes

Definition

def greet(name):

Creates the function, does not run it

Call

greet("Ada")

Executes the function body

Parameter

name

Placeholder in the definition

Argument

"Ada"

Actual value passed at call time

Return

return value

Sends a value back, exits immediately

No return

(nothing) or return

Implicitly returns None

Example
A function to calculate the average of a list of numbers, with a docstring and an early return for the empty case.

Python
def average(numbers):
    """Return the average of a list of numbers, or None if empty."""
    if not numbers:
        return None
    return sum(numbers) / len(numbers)


print(average([4, 8, 15, 16, 23, 42]))
print(average([]))
18.0
None