Python Functions: Definitive Guide & Examples

Master Python functions. Learn how to write reusable code using def, define arguments, return values, and understand scope locally and globally.

Try Python Functions Code

Overview

A function is a self-contained block of organized, reusable code designed to perform a single, specific action. As your Python scripts grow beyond a few lines, organizing code into functions becomes critical. Functions abstract away complexity, making your main program flow easier to read and maintain. Instead of repeating the same logic in multiple places, you write a function once and "call" it wherever needed.

Python provides numerous built-in functions out of the box, such as `print()`, `len()`, and `type()`. However, the true power of programming comes from creating your own user-defined functions. By encapsulating logic, you create modular building blocks that can be tested in isolation and reused across different projects or shared with a team.

To define a function in Python, you use the `def` keyword, followed by a descriptive function name, parentheses `()`, and a colon `:`. Any indented code below this definition belongs to the function block. The function executes only when it is explicitly called by its name.

Functions can accept data through parameters (also called arguments). These are variables listed inside the parentheses of the function definition. You can require positional arguments, which must be passed in exact order, or use keyword arguments with default values, allowing for flexible function calls. For instance, `def calculate_tax(price, rate=0.05):` allows the caller to omit the `rate` argument, defaulting it to 5%.

Finally, functions typically send data back to the caller using the `return` statement. When a `return` is executed, the function immediately terminates and passes the specified value back. In Python, you can even return multiple values separated by commas, which Python automatically bundles into a tuple. Understanding function scope—the rule that variables created inside a function cannot be accessed from outside it—is vital for preventing bug-prone state mutations.

Code Example

Here is a simple function that calculates the tax applied to a price, demonstrating parameters and return values.

functions_demo.py
Try in Editor
def calculate_total(price, tax_rate=0.08):
    """
    Calculates the final price after applying tax.
    """
    tax_amount = price * tax_rate
    total = price + tax_amount
    return total

print(calculate_total(100.00))
print(calculate_total(50.00, tax_rate=0.10))
Terminal Output
108.0
55.0

Real-world Use Cases

  • Encapsulating complex mathematical formulas
  • Separating UI logic from backend business logic
  • Creating utility modules for team collaboration

Frequently Asked Questions

What are default arguments?

Default arguments are values that are used if no argument is provided during the function call.

Can a function return multiple values?

Yes, Python allows functions to return multiple values by separating them with commas. Under the hood, this returns a tuple.

Keep Learning

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.