When writing code, you often need to run the same block of logic multiple times. Instead of rewriting the same lines over and over, Python allows you to group that code into a reusable block called a function.
In this step-by-step guide, you will learn exactly how to define a function in Python, understand its core syntax, and see real-world examples updated for 2026 standards.
What is a Function in Python?
A function is a self-contained block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.
How to Define a Function in Python (The Syntax)
To optimize for Google’s Featured Snippet, here is the exact structural anatomy of a Python function:
python
def function_name(parameters):
"""Optional: Docstring to explain what the function does"""
# Function body (Indented code block)
return value
Use code with caution.
The 4 Core Components Explained:
- The
defKeyword: This tells Python that you are about to define a new function. - Function Name: A unique name written in
snake_caselowercase words separated by underscores. - Parentheses
(): This is where you place any inputs (parameters) your function needs. Even if it takes no inputs, the parentheses are mandatory. - The Colon
:: This marks the end of the function header and the start of the execution block.
Step-by-Step Examples of Python Functions
Step 1: Defining a Simple Function (No Arguments)

If your function does not require any external data, keep the parentheses empty.
python
def greet_user():
print("Welcome to Python coding in 2026!")
# Calling the function
greet_user()
Use code with caution.
Output:Welcome to Python coding in 2026!
Step 2: Adding Parameters and Arguments

Parameters allow your function to accept custom data.
python
def celebrate_achievement(name, course):
print(f"Congratulations {name}! You completed the {course} tutorial.")
# Passing arguments into the function
celebrate_achievement("Alex", "Python Basics")
Use code with caution.
Output:Congratulations Alex! You completed the Python Basics tutorial.
Step 3: Using the return Statement & Real-World Cheat Sheet (2026)

If you want your function to send data back to your main script, use the return keyword. Here is a complete professional example showing Default Arguments, Type Hinting, and Return Values:
python
def calculate_discount(price: float, discount_percentage: float = 10.0) -> float:
"""
Calculates the final price of an item after applying a discount.
Default discount is 10% if not specified.
"""
discount_amount = price * (discount_percentage / 100)
final_price = price - discount_amount
return final_price
# Scenario 1: Using the default discount (10%)
shirt_price = calculate_discount(1000)
print(f"Price with default discount: ₹{shirt_price}") # Output: ₹900.0
# Scenario 2: Overriding the default discount with a custom 25% discount
laptop_price = calculate_discount(50000, 25)
print(f"Price with special discount: ₹{laptop_price}") # Output: ₹37500.0
Use code with caution.
Common Mistakes Beginners Make (And How to Avoid Them)
To save time debugging, keep an eye out for these frequent beginner errors:
- Indentation Errors: Python relies heavily on indentation (usually 4 spaces). If your function body isn’t properly indented, Python will throw an
IndentationError. - Forgetting the Colon: Always end your
def function_name()line with a single colon (:). - Confusing Arguments vs Parameters: Parameters are the variables listed inside the function definition. Arguments are the actual values you pass into the function when calling it.
Python Function Components Quick Summary
| Component | Keyword / Syntax | Purpose | Example |
|---|---|---|---|
| Declaration | def | Tells Python to start a function | def calculate_bill(): |
| Input Placeholders | Parameters | Receives data from outside | def greet(name, age): |
| Output Sender | return | Sends data back to the script | return total_price |
| Default Output | None | Returned automatically if no return is used | Implicit |
Frequently Asked Questions (FAQs)
Q1. What is the difference between a function and a method in Python?
Ans: A function is a block of code written outside of any class and can be called independently by its name (e.g., print()). A method is a function that is defined inside a class and can only be called on an object associated with that class (e.g., list.append()).
Q2. Can a Python function return multiple values?
Ans: Yes, Python functions can return multiple values separated by commas. When you return multiple items, Python packages them together and returns them as a single Tuple.
Example:return width, height
Q3. What happens if a function does not have a return statement?
Ans: If a function does not explicitly use a return statement, it automatically returns None by default. The function will execute its code block, but any variable trying to capture its output will receive None.
Q4. What is the difference between parameters and arguments?
Ans:
- Parameters are the placeholders or variables listed inside the parentheses during the function’s definition (e.g.,
def greet(name):). - Arguments are the actual values passed into the function when you invoke or call it (e.g.,
greet("Alice")).
Q5. Can I define a function inside another function in Python?
Ans: Yes, Python supports nested functions. A function defined inside another function is called an inner or nested function. It is commonly used for creating closures and decorators.
Conclusion
Mastering how to define a function in Python is one of the most critical steps to becoming a proficient developer. It makes your code cleaner, faster, and easier to debug.
