Last modified: Nov 26, 2026
Python def: Meaning and How It Works
The word def is one of the first keywords you meet in Python. It stands for "define". You use it to create a function. A function is a named block of code that you can run whenever you need it.
This guide explains what def means, how to write a function, and how to avoid common mistakes. It is written for beginners who want a clean and simple answer.
What Does def Mean in Python?
def is a keyword that starts a function definition. When Python sees def, it knows you are about to name a new function and describe what it does.
The function body does not run when you define it. It only runs when you call the function. This is a key idea. Defining is not the same as running.
Here is the smallest possible example:
def greet():
print("Hello, world!")
greet()
Hello, world!
The line def greet(): creates the function. The indented line is the body. The call greet() runs it.
The Basic Syntax of a Function
A function definition has a few simple parts. First comes the def keyword. Then the function name. Then a pair of parentheses. Then a colon.
The body must be indented. Python uses indentation to group code. Four spaces is the common choice. Be consistent across your files.
def add(a, b):
# a and b are parameters
result = a + b
return result
print(add(3, 5))
8
Inside the parentheses you list parameters. When you call the function, you pass arguments. The return statement sends a value back to the caller.
Parameters and Arguments
Parameters make functions flexible. You can pass different values each time you call the function. This avoids repeating code.
Python supports several kinds of parameters. The most common are positional and keyword arguments.
def describe(name, age):
print(f"{name} is {age} years old.")
describe("Ana", 30) # positional
describe(age=25, name="Ben") # keyword
Ana is 30 years old.
Ben is 25 years old.
You can also set default values. A default is used when the caller does not provide that argument.
def welcome(name, greeting="Hello"):
print(f"{greeting}, {name}!")
welcome("Sam")
welcome("Sam", "Hi")
Hello, Sam!
Hi, Sam!
Return Values vs. Printing
Beginners often mix up print and return. They are not the same. print shows text on the screen. return hands a value back to the code that called the function.
If a function has no return statement, it returns None. That is a useful fact to remember.
def show(x):
print(x) # prints, returns None
def double(x):
return x * 2 # returns a value
print(show(4))
print(double(4))
4
None
8
Scope: Where Variables Live
Variables created inside a function are local. They exist only while the function runs. This keeps your code clean and avoids accidental clashes.
def counter():
count = 0
count += 1
return count
print(counter())
print(counter())
# print(count) # NameError: count is local
1
1
Each call starts fresh. If you need to keep a value between calls, use a global variable, a default argument, or a class. Classes are a natural next step, and you can learn how constructors work in this beginner's guide to Python __init__.
Functions as Objects
In Python, functions are objects. You can store them in variables, pass them to other functions, and return them. This is the base of decorators and callbacks.
def shout(text):
return text.upper()
def run(func, value):
return func(value)
print(run(shout, "hello"))
HELLO
Because functions are objects, you can wrap them with decorators. A decorator takes a function and returns a new one. This pattern is everywhere in Python. If you want to go deeper, read the Python class decorator guide.
Docstrings and Readability
A good function has a clear name and a short docstring. A docstring is a string on the first line of the body. It explains what the function does.
def area(width, height):
"""Return the area of a rectangle."""
return width * height
print(area(4, 5))
print(area.__doc__)
20
Return the area of a rectangle.
Keep functions small. Do one thing well. Short functions are easier to test and easier to read.
Common Mistakes with def
Forgetting the colon. The line must end with :. Without it, Python raises a SyntaxError.
Wrong indentation. The body must be indented. Mixed tabs and spaces cause errors. Pick one style and stick to it.
Calling before defining. The function must be defined before the call runs. Place definitions above the code that uses them.
Missing parentheses. Writing greet gives you the function object. Writing greet() calls it. This is a very common bug.
def greet():
return "Hi"
print(greet) # shows the function object
print(greet()) # calls it
Hi
def Inside Classes
You can also use def inside a class. These are called methods. The first parameter is usually self, which refers to the object.
class Dog:
def bark(self):
return "Woof!"
d = Dog()
print(d.bark())
Woof!
Classes often use a special method to set up objects. You can review it in the Python class __init__ function article. Not every class needs one, and this explanation of a Python class without __init__ shows when you can skip it.
Quick Recap
def means "define a function". You write the keyword, a name, parentheses, and a colon. Then you indent the body. Call the function with its name and parentheses.
Use return to send values back. Use parameters to make functions flexible. Keep names clear and functions short. These habits will serve you well as your programs grow.
Conclusion
The meaning of def in Python is simple. It creates a reusable block of code. Once you understand definition, calling, parameters, and return values, you can build larger programs with confidence.
Start with small functions. Test them often. Read the errors carefully. With a little practice, def will feel natural and your code will become cleaner and easier to maintain.