Python Functions: How to Define and Call Them
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 Python for Beginners. 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?
Click to play video
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:
ris a parameter — a variable that receives input when the function is called- The indented lines are the function body
returnsends 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
def area_of_circle(r):— the function is defined, but not called yet. The body is skipped for now.radius = 5— a new variable is created.area_of_circle(radius)— the function is called with5as the argument. Now we jump into the body.ris set to5.pi = 3.14andresult = pi * r * r— the math runs,resultis78.5.return result—78.5is sent back to the caller.area = area_of_circle(radius)— the returned78.5is stored inarea.print(area)— prints78.5.
Use the interactive demo below to call the function yourself and watch the body execute line by line. It's a hands-on visualization of how a Python function call actually runs:
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.returnends 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
Default Parameters and Multiple Return Values
Python functions can make parameters optional with default values and return more than one value at a time. Those features have a few rules worth knowing, so they're covered separately in Python default parameters and returning multiple values from Python functions.
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.
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.
How do you call a function in Python?
Write the function name followed by parentheses containing any arguments. For example, area_of_circle(5) calls area_of_circle with 5 as its argument.
