Learn Python / Functions

Functions

You've been using functions since your first lesson: print(), len(), input(), round(). Now you'll write your own. A function is a named block of code that does one job. You define it once, then call it as many times as you like.

Defining and calling a function

Use def, a name, parentheses and a colon. The indented block below is the function's body:

def greet():
    print("Hello!")
    print("Welcome to Python.")

greet()
greet()
Output

Defining a function doesn't run it. The body only runs when you call the function by writing its name followed by ().

Function names follow the same rules as variable names. By convention they're snake_case and usually start with a verb: calculate_total, send_email, is_valid.

Parameters: giving a function input

Put parameters inside the parentheses. They act like variables that get their values when the function is called:

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

greet("Ada")
greet("Grace")
Output

The values you pass in, "Ada" and "Grace", are called arguments. A function can take several parameters, separated by commas:

def describe_pet(name, animal):
    print(f"{name} is a {animal}.")

describe_pet("Rex", "dog")
describe_pet("Tom", "cat")
Output

Passing the wrong number of arguments raises a TypeError:

def describe_pet(name, animal):
    print(f"{name} is a {animal}.")

describe_pet("Rex")
Output

return: getting a result back

So far our functions only print. Most useful functions return a value instead, so the caller can store it, use it in a calculation or pass it on:

def square(n):
    return n * n

result = square(5)
print(result)
print(square(3) + square(4))
Output

return ends the function immediately and sends the value back to wherever the function was called.

This difference trips up many beginners. print shows something on the screen, while return hands a value back to your program:

def add_print(a, b):
    print(a + b)

def add_return(a, b):
    return a + b

x = add_print(2, 3)    # shows 5, but x gets nothing
y = add_return(2, 3)   # shows nothing, but y is 5

print("x is", x)
print("y is", y)
Output

A function without a return statement returns None.

Returning early

return can appear anywhere in the body, which makes it easy to handle special cases first:

def divide(a, b):
    if b == 0:
        return "Can't divide by zero"
    return a / b

print(divide(10, 2))
print(divide(5, 0))
Output

Returning several values

Return several values separated by commas. Python packs them into a tuple, which you can unpack straight away:

def min_max(numbers):
    return min(numbers), max(numbers)

low, high = min_max([7, 2, 9, 4])
print(low, high)
Output

Docstrings

A docstring is a string on the first line of a function that explains what it does. Tools and editors show it as help text:

def area_of_circle(radius):
    """Return the area of a circle with the given radius."""
    import math
    return math.pi * radius ** 2

print(round(area_of_circle(3), 2))
help(area_of_circle)
Output

Functions calling functions

Functions can call other functions. Small functions that each do one thing combine into bigger ones:

def celsius_to_fahrenheit(c):
    return c * 9 / 5 + 32

def describe_temperature(c):
    f = celsius_to_fahrenheit(c)
    if c >= 25:
        feel = "hot"
    elif c >= 15:
        feel = "mild"
    else:
        feel = "cold"
    return f"{c}°C ({f:.0f}°F) feels {feel}"

print(describe_temperature(30))
print(describe_temperature(8))
Output

Why use functions?

  • Reuse: write the logic once, call it everywhere.
  • Readability: calculate_tax(price) explains itself far better than the arithmetic inside it.
  • Easier fixes: if there's a bug, you fix it in one place.
  • Testing: a small function with clear input and output is easy to check.

Exercises

Exercise 1: Rectangle area

Write a function rectangle_area(width, height) that returns the area. The code below should then print 24 and 50.

# define rectangle_area here

print(rectangle_area(4, 6))
print(rectangle_area(10, 5))
Output

Exercise 2: Is it even?

Write a function is_even(n) that returns True if n is even and False otherwise.

# define is_even here

print(is_even(4))
print(is_even(7))
Output

Exercise 3: Average of a list

Write average(numbers) that returns the average of a list, or 0 if the list is empty.

# define average here

print(average([4, 8, 6]))
print(average([]))
Output

Exercise 4: Grade function

Turn the grading logic from the If / Elif / Else lesson into a function get_grade(score) that returns "A" for 90 and over, "B" for 80 and over, "C" for 70 and over, and "F" otherwise. Then use a loop to print each score followed by its grade, like 95 A.

# define get_grade here

scores = [95, 83, 71, 40]
Output

Summary

  • def name(parameters): defines a function, and name(arguments) calls it.
  • Parameters are the names in the definition. Arguments are the values you pass in.
  • return sends a value back and ends the function. Without it, a function returns None.
  • print shows a value, while return gives it back to your program.
  • Return several values with commas and unpack them: low, high = min_max(nums).
  • A docstring on the first line documents what the function does.