AlgoMaster Logo

Functions Basics

Last Updated: January 3, 2026

5 min read

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.

What is a Function?

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 Functions

Defining a function in Python is straightforward. The general syntax looks like this:

Function Definition Structure

  1. Keyword: The def keyword signals the start of a function definition.
  2. Function Name: This should be descriptive to indicate what the function does.
  3. Parameters: These are the inputs to the function, enclosed in parentheses. You can have zero or more parameters.
  4. Body: This is where the code that performs the function's task is written.
  5. Return Statement: This is optional. If included, it sends a value back to the caller.

Example 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.

Function Calls

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.

Calling a Function

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.

Why Use Functions?

Functions provide several benefits:

  • Reusability: You can call the same function multiple times with different arguments.
  • Organization: Breaking code into smaller chunks makes it more manageable and readable.
  • Abstraction: Functions allow you to hide complex logic behind a simple interface.

Local and Global Variables

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.

Example of Local vs. Global Variables

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.

Side Effects

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.

Docstrings: Documenting Functions

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.

Using Docstrings

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.

Higher-Order Functions

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.

Example of a Higher-Order Function

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.

Use Cases

  • Callbacks: Functions that get called after a certain event.
  • Functional Programming: Techniques such as mapping, filtering, and reducing collections of data.

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.