def in Python is the keyword used to define a function. It turns reusable code into a named block with optional parameters, so you can call it whenever you need it.
What def means in Python
def in Python marks the start of a function definition, which is a named piece of code with its own parameters and body. Functions help avoid repeating the same logic and make programs easier to read.
The basic structure looks like this:
- def — starts the function definition;
- function_name — the name used to call it;
- (parameters) — input values, if the function needs them;
- : — the colon that begins the code block;
- indentation — the function body.
Example:
def greet(name):
print(f"Hello, {name}!")
How a function defined with def works
A function defined with def in Python stores instructions until it is called, instead of running them as soon as the file loads. That makes it useful when the same action is needed in different parts of a program.
After the definition, the function is called by name:
greet("Olena")
If the call is correct, the greeting appears on the screen. The check is simple: if the output matches what you expected, the function works; if not, first verify the function name, the number of arguments, and the indentation.
Parameters, return, and scope
Parameters in def in Python pass data into a function, while return sends a result back out. Without return, a function can still perform an action, such as printing text or changing program state.
Example of a function that returns a value:
def add(a, b):
return a + b
sum_result = add(3, 5)
Scope matters too: variables created inside a function are usually not available outside it unless they are returned explicitly. That reduces accidental errors and makes code more predictable.
Common def mistakes and how to check them quickly
Common def mistakes in Python usually involve syntax, indentation, or calling the function with the wrong arguments. These issues are easy to miss, especially when you are just starting out.
- Missing a colon after the function header.
- Incorrect indentation in the function body.
- Calling the function without required arguments or with too many.
- No return statement when a value is expected.
The check is straightforward: if Python raises a SyntaxError, the problem is in the code structure; if the code runs but the result is wrong, review the parameters, return value, and logic inside the function. A safer approach is to start with a tiny 2- or 3-line example and expand it only after it works.
When def is better than writing code inline
def in Python is especially useful when one action repeats several times or when a program needs a clear structure. Functions make code easier to test, reuse, and maintain.
- processing user input;
- calculating sums, averages, or checks;
- working with files and data;
- breaking larger logic into clear, manageable parts.
If code starts repeating, that is the clearest sign it should be moved into a function with def. This approach makes a program shorter, cleaner, and easier to change.

