Python Functions: How to Define and Call Them

Boot.dev Blog » Python » Python Functions: How to Define and Call Them
Lane Wagner
Lane Wagner

Last published March 1, 2026

Table of Contents

Functions allow you to reuse and organize code. Instead of copying and pasting the same logic everywhere, you define it once and call it whenever you need it. They’re the most important tool for writing DRY code.

All the content from our Boot.dev courses are available for free here on the blog. This one is the “Functions” and “Scope” chapters of Learn to Code in Python. If you want to try the far more immersive version of the course, do check it out!

How Do You Define a Function in Python?

You define a function using the def keyword, followed by the function name, parentheses with any parameters, and a colon. The indented lines below form the function body — the code that runs each time the function is called.

def area_of_circle(r):
    pi = 3.14
    result = pi * r * r
    return result

Here’s what’s happening:

  • r is a parameter — a variable that receives input when the function is called
  • The indented lines are the function body
  • return sends a value back to the caller

To call the function, use its name and pass in an argument:

area = area_of_circle(5)
print(area)  # 78.5

Because we’ve defined the function, we can reuse it with different inputs:

print(area_of_circle(6))   # 113.04
print(area_of_circle(7))   # 153.86

How Does a Function Call Work Step by Step?

Let’s trace through a complete example so you can see the exact order of execution:

def area_of_circle(r):
    pi = 3.14
    result = pi * r * r
    return result

radius = 5
area = area_of_circle(radius)
print(area)  # 78.5
  1. def area_of_circle(r): — the function is defined, but not called yet. The body is skipped for now.
  2. radius = 5 — a new variable is created.
  3. area_of_circle(radius) — the function is called with 5 as the argument. Now we jump into the body.
  4. r is set to 5.
  5. pi = 3.14 and result = pi * r * r — the math runs, result is 78.5.
  6. return result78.5 is sent back to the caller.
  7. area = area_of_circle(radius) — the returned 78.5 is stored in area.
  8. print(area) — prints 78.5.

What Is the Difference Between Print and Return?

New developers often confuse print() and return. They are not interchangeable.

  • print() writes a value to the console. It does not send anything back to the caller.
  • return ends the function and provides a value back to whoever called it. It does not print anything.
def get_greeting(name):
    return f"Hello, {name}"

message = get_greeting("Lane")
print(message)  # Hello, Lane

If you replaced return with print inside the function, message would be None — because print() doesn’t return the string, it just displays it.

Use print() for debugging while you write code. Use return to send data between functions. That’s the rule.

How Do You Use Multiple Parameters?

Functions can accept more than one parameter. It’s the argument’s position that determines which parameter receives it:

def subtract(a, b):
    return a - b

result = subtract(5, 3)
print(result)  # 2

5 goes to a, 3 goes to b. Here’s one with four parameters:

def create_intro(name, age, height, weight):
    first = f"Your name is {name} and you are {age} years old."
    second = f"You are {height}m tall and weigh {weight}kg."
    return first + " " + second

Can a Function Return Multiple Values?

Yes. Separate the return values with commas:

def cast_iceblast(wizard_level, start_mana):
    damage = wizard_level * 2
    new_mana = start_mana - 10
    return damage, new_mana

When calling, assign them to multiple variables:

damage, mana = cast_iceblast(5, 100)
print(f"Damage: {damage}, Remaining Mana: {mana}")
# Damage: 10, Remaining Mana: 90

The order of the values matters, not the variable names. The first returned value goes to the first variable, the second to the second, and so on.

What Are Default Parameter Values?

You can give a parameter a default value so it becomes optional. Use the = operator in the function signature:

def get_greeting(email, name="there"):
    print(f"Hello {name}, welcome! Email: {email}")
get_greeting("[email protected]", "Lane")
# Hello Lane, welcome! Email: [email protected]

get_greeting("[email protected]")
# Hello there, welcome! Email: [email protected]

If the caller doesn’t provide name, it defaults to "there". Parameters with defaults must come after all required parameters.

What Happens When a Function Doesn’t Return Anything?

If a function has no return statement (or just a bare return), it returns None. These three functions all behave identically:

def my_func():
    print("I do nothing")
    return None
def my_func():
    print("I do nothing")
    return
def my_func():
    print("I do nothing")

This matters when you try to use the “result” of a function that doesn’t actually return one — you’ll get None instead of a real value.

Where Should You Define Functions in Python?

Code executes top to bottom, so a function must be defined before it’s called:

print(my_name)
my_name = "Lane Wagner"
# NameError: 'my_name' is not defined

The same applies to functions. You might think this makes ordering your code annoying, but there’s a simple trick: define all your functions first, then call an entry point function last. By convention, this function is called main:

def main():
    health = 10
    armor = 5
    add_armor(health, armor)

def add_armor(h, a):
    new_health = h + a
    print_health(new_health)

def print_health(new_health):
    print(f"The player now has {new_health} health")

main()

This works because by the time main() is called on the last line, every function has already been defined. The order of the def statements relative to each other doesn’t matter — only that they all come before the first call.

What Is Variable Scope in Python?

Scope refers to where a variable is available to be used. When you create a variable inside a function, it only exists within that function:

def subtract(x, y):
    return x - y

result = subtract(5, 3)
print(x)
# NameError: name 'x' is not defined

The variable x only lives inside subtract. Trying to use it outside raises an error.

Variables defined outside any function live in the global scope and are accessible everywhere, including inside functions:

pi = 3.14

def get_area_of_circle(radius):
    return pi * radius * radius

Because pi was declared in the global scope, it’s usable inside get_area_of_circle(). Understanding scope prevents a lot of confusing bugs — if you’re getting a NameError, check whether the variable you’re trying to use is actually in scope.

What Should You Learn After Python Functions?

Functions are the building block of every Python program. Now that you understand how to define, call, and compose them, you’re ready to start using them with loops and lists to write more powerful programs.

If you want to go deeper on function design patterns like pure functions, closures, and lambdas, check out our post on all the kinds of functions in Python.

Frequently Asked Questions

What is the difference between parameters and arguments?

Parameters are the names used for inputs when defining a function. Arguments are the actual values you pass in when calling the function. That said, most developers use the two words interchangeably.


What does a Python function return if there is no return statement?

It returns None. Whether you write return with no value, return None, or omit the return statement entirely, the result is the same: the function returns None.


Can you use a variable defined inside a function outside of it?

No. Variables created inside a function only exist within that function's scope. Trying to access them outside will raise a NameError.


Can a Python function return more than one value?

Yes. Separate the values with commas in the return statement and assign them to multiple variables when calling the function.