This text is part of a Real Python tutorial by Leodanis Pozo Ramos.
A Python function is a named block of code that performs specific tasks and can be reused in other parts of your code. Python has several built-in functions that are always available, and you can also create your own. These are known as user-defined functions.
To define a function in Python, you use the def keyword, followed by the function name and an optional list of parameters enclosed in a required pair of parentheses. You can call and reuse a function by using its name, a pair of parentheses, and the necessary arguments.
Learning to define and call functions is a fundamental skill for any Python developer. Functions help organize your code and make it more modular, reusable, and easier to maintain.
Defining Functions in Python
It’s time to start defining your own functions in Python. The general syntax for defining a function is shown below:
def function_name([parameters]):
<block>
Below, you have a breakdown of this syntax’s components:
| Component | Description |
|---|---|
def |
The keyword that begins the function definition |
function_name |
A valid Python identifier that names the function |
[parameters] |
An optional, comma-separated list of formal parameters for the function |
: |
The punctuation that denotes the end of the function header |
<block> |
The function’s code block |
The final item, <block>, is usually called the function’s body. The body is a series of statements that will run when you call the function. The body is defined by indentation. This is the same as code blocks associated with a control structure, like an if or while statement.
Here’s your first Python function:
>>> def greet(name):
... print(f"Hello, {name}!")
...
>>> greet("Pythonista")
Hello, Pythonista!
You start the function’s definition with the def keyword. Then, you have the function name, greet, followed by a pair of parentheses enclosing an argument called name. To close the function header, you use a colon (:).
Next, you have an indented code block consisting of a call to the built-in print() function. This call will display a greeting message on your screen. Finally, you call the function using "Pythonista" as an argument. As a result, you get Hello, Pythonista! printed on the screen.
Note: Occasionally, you may want to define a function that does nothing. This is referred to as a stub, which is usually a temporary placeholder for a Python function that will be fully implemented at a later time. To define a stub function in Python, you use the pass statement, or in some contexts, a Python ellipsis (...):
>>> def function():
... pass
...
>>> function()
As you can conclude from this example, this stub function is syntactically valid but doesn’t do anything when you call it. You need the pass statement to make the function syntactically correct, as function bodies can’t be empty.
Now you know the basics of defining functions. In the following section, you’ll learn more about different ways to call functions in Python.
Calling Functions in Python
You’ve already called a few functions so far. The syntax consists of writing the function’s name followed by a pair of parentheses, which encloses an optional series of arguments:
function_name([arguments])
Here, <arguments> represent the concrete values that you pass into the function. They correspond to the <parameters> in the Python function definition.
Note: There’s a subtle but important distinction between the terms parameter and argument. Parameters are those names used in the function definition, while arguments are the concrete values that you supply for each parameter in the function call.
Not all functions need arguments. However, the parentheses are required when you intend to call the function, even if the function takes no arguments. Both function definitions and calls must always include parentheses, even when they’re empty.
You’ll get a syntax error if you forget the parentheses in a function definition. If you forget the parentheses in a function call, then you’ll get a function object instead of the expected result that the function should produce. The name of the function refers to the function itself, but adding parentheses tells Python to execute the code within the function and return a value.
Note: Forgetting the parentheses in a function call might be a common mistake when you’re starting out with Python. So, if you intend to call a function, make sure you add the parentheses. Otherwise, you could end up with unexpected behavior and a hard-to-track bug.