Skip to content
Codewilds

Lesson 9 of 34 · Functions

Python Functions: def, Parameters and return

easy10 XP3 min read

You've already filled in a few function bodies. Now it's time to understand functions properly — they are the single most important tool for organising code. A function is a named, reusable block of code: you write the logic once, give it a clear name, and call it whenever you need it.

Functions make programs shorter (no copy-paste), easier to read (a good name explains intent) and easier to test (each function can be checked on its own — exactly what the tests in this course do).

Defining and calling a function

def greet(name):
    return f"Welcome to Serpent Hollow, {name}!"


message = greet("Ada")
print(message)  # Welcome to Serpent Hollow, Ada!
  • def starts the definition, followed by the function's name and its parameters in parentheses, then a colon.
  • The indented block is the function body. It doesn't run when Python reads the definition — only when the function is called.
  • greet("Ada") calls the function. The value "Ada" is the argument; inside the body it is available through the parameter name.

A function can take any number of parameters, including none: def roll():.

return sends a value back

return ends the function and hands a value back to the caller, who can store it, print it or use it in further calculations:

def area(width, height):
    return width * height


total = area(2, 3) + area(4, 5)
print(total)  # 26

This is the key difference between returning and printing. print() only shows text on the screen; the caller gets nothing it can compute with. Most functions should return their result and let the caller decide what to do with it.

No return means None

A function without a return statement (or with a bare return) gives back None:

def shout(text):
    print(text.upper())


result = shout("hi")  # prints HI
print(result)         # None

return exits immediately

As soon as return runs, the function stops. This allows early returns that handle special cases first:

def describe_hp(hp):
    if hp <= 0:
        return "defeated"
    if hp < 30:
        return "wounded"
    return "healthy"

Functions can call other functions

Small functions combine into bigger ones. Each piece stays simple and testable:

def square(n):
    return n * n


def sum_of_squares(a, b):
    return square(a) + square(b)


print(sum_of_squares(3, 4))  # 25

Docstrings and type hints

A string on the first line of the body is a docstring — documentation that editors and help() display. Type hints describe what types a function expects and returns:

def average(a: float, b: float) -> float:
    """Return the average of two numbers."""
    return (a + b) / 2

Type hints are documentation for humans and tools; Python doesn't enforce them when the code runs.

Common mistakes

  • Printing instead of returning: the tests (and other code) receive None.
  • Forgetting the parentheses: print(greet) shows something like <function greet at 0x...> instead of calling the function.
  • Code after return in the same block never runs.
  • Calling a function before it is defined: Python reads the file top to bottom, so a call that runs before the def line raises NameError.

Assignment

Write two functions for the Hollow's weather station:

  1. to_fahrenheit(celsius) returns the temperature converted to Fahrenheit using celsius * 9 / 5 + 32.
  2. format_temperature(celsius) returns a string like "25.0°C is 77.0°F", with both numbers shown with one decimal place. It must call to_fahrenheit() rather than repeating the formula.
to_fahrenheit(100)        # 212.0
format_temperature(25)    # "25.0°C is 77.0°F"
format_temperature(-3.5)  # "-3.5°C is 25.7°F"

Both functions must return their result, not print it. (The degree sign ° is in the starter code, ready to copy.)

Premium lesson

Unlock the full Learn Python course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Functions and methods in other languages

Need a quick reminder? The Python cheat sheet has every topic of Learn Python on one page.

Lesson last updated on .

Write code ↓
Python+10 XP

Lesson 9 is part of the full course. The first 8 lessons are free.

Premium lesson

Unlock the full Learn Python course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.