Defining & Calling Functions

How do you package a block of reusable code so it can be called multiple times with different inputs? Functions provide a structured way to encapsulate code, allowing you to define a block once and execute it repeatedly with varying data.

The def Keyword: Your Function's Blueprint

In Python, you define a function using the def keyword, followed by the function's name and a pair of parentheses. These parentheses hold any parameters the function might accept. A colon : marks the end of the function header, and the function's body — the code it executes — is written on subsequent lines, indented by four spaces. This indentation is critical, as it tells Python which statements belong to the function.

pythonDefining and Calling a Basic Function
Try It Yourself
Define a new function called say_goodbye that prints "Goodbye for now!" and then call it.
python

Passing Data In: Parameters and Arguments

Functions become truly powerful when they can operate on different pieces of data each time they are called. Parameters are placeholders defined in the function's signature, acting as variables that will receive input. When you call a function, the actual values you pass into these parameters are called arguments. Python matches arguments to parameters either by their position (positional arguments) or by explicitly naming the parameter (keyword arguments).

pythonFunctions with Flexible Inputs
Check Your Understanding
Given def process_data(a, b, c):, which call will result in an error?

Getting Results Out: The return Statement

While print() displays information to the console, the return statement allows a function to send a value back to the part of the code that called it. This returned value can then be stored in a variable, used in an expression, or passed as an argument to another function. When a return statement is executed, the function immediately stops, and the specified value is sent back. If a function doesn't explicitly return a value, it implicitly returns None.

pythonCapturing Function Outputs
Check Your Understanding
After executing the code, what value will output_a and output_b hold?

Explaining Your Code: Docstrings

Good code is not just functional; it's also understandable. Docstrings are multi-line string literals, enclosed in triple quotes ("""Docstring goes here"""), placed immediately after the function's def line. They serve as built-in documentation, explaining what the function does, its parameters, and what it returns. Docstrings are accessible programmatically via help(function_name) or function_name.__doc__, making your code easier for others (and your future self) to use and maintain.

pythonDocumenting Your Reusable Logic
Try It Yourself
Add a docstring to the multiply function below. The docstring should explain that it multiplies two numbers, list its num1 and num2 arguments, and state that it returns their product. Then, use print(multiply.__doc__) to verify your docstring.
python

Why Functions Drive Better Code

Functions are more than just a way to avoid copying and pasting code; they fundamentally improve code quality. They promote modularity, breaking down complex problems into smaller, manageable units. This makes code more readable, as each function has a clear, single responsibility. When an issue arises, functions make debugging easier by isolating the problem to a specific, smaller block of code. Finally, they enable abstraction, allowing you to use a function without needing to understand its internal implementation details, focusing instead on what it does.

Key Takeaways
  • Define functions using the def keyword, followed by the function name, parentheses, and a colon.

  • The function body must be indented, containing the code to be executed.

  • Parameters are placeholders in the definition; arguments are the actual values passed during a call.

  • Use the return statement to send a value back from the function to the caller.

  • Docstrings (triple-quoted strings) immediately after def provide essential documentation, accessible via .__doc__ or help() function.

  • Functions are the primary mechanism to package reusable code, improving modularity, readability, and maintainability.

← All lessons in Python Basics & Syntax

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free