Last Updated: January 3, 2026
Functions are one of the cornerstones of programming in Python. They allow us to encapsulate code into reusable blocks, making our programs more organized and efficient. Whether you're building a simple script or a complex application, understanding how to define and use functions is crucial.
Let’s dive into the basics of functions in Python, including how to define them, how they work, and the key features that make them so powerful.
At its core, a function is a named block of code designed to perform a specific task. By grouping code into a function, you can run that code whenever you need it, which helps to avoid repetition and improve clarity.
Here’s a simple example of a function that adds two numbers:
In this example, add_numbers is the name of the function, and it takes two parameters, a and b. When we call the function with add_numbers(5, 3), it returns the sum of these two numbers.
Defining a function in Python is straightforward. The general syntax looks like this:
def keyword signals the start of a function definition.Here's a more elaborate function example that checks if a number is even:
This function checks if a number is even by using the modulus operator %. If the number is divisible by 2, it returns True; otherwise, it returns False.
Once you've defined a function, you can call it by using its name followed by parentheses. If the function requires parameters, you need to provide the appropriate arguments within those parentheses.
In this example, the greet function takes one parameter, name, and returns a greeting string. When we call greet("Alice"), it outputs a personalized greeting.
Functions provide several benefits:
When working with functions, understanding scope is important. Variables defined inside a function are local to that function and cannot be accessed outside it. Conversely, global variables are defined outside of functions and can be accessed anywhere in the module.
In this case, x is a global variable, while y is local to function(). Attempting to access y outside the function would lead to a NameError.
Be careful with global variables, as modifying them inside a function can lead to unexpected behavior. It's often best to return a value from a function rather than changing global state.
Good documentation is crucial for any piece of code, and functions are no exception. Python provides a way to document functions using docstrings. A docstring is a string literal that occurs as the first statement in a function. It describes what the function does.
Here’s how to add a docstring to our add_numbers function:
This documentation can be accessed via the function’s __doc__ attribute and is invaluable for helping others (and yourself) understand the function's purpose.
In Python, functions can also accept other functions as arguments or return them as results. This concept is known as higher-order functions. It allows for more flexible and powerful programming patterns.
In this example, apply_function takes another function (func) as an argument and applies it to value. This pattern allows you to create more dynamic and reusable code.
Functions are foundational to writing effective Python code. They encapsulate logic, promote reusability, and enhance code clarity.
Now that you understand the basics of functions, you are ready to explore function parameters.
In the next chapter, we will look at how to pass information into functions, which will allow you to create even more dynamic and flexible code.