Defining Your Own Python Function

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:

Language: Python Syntax
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:

Language: Python
>>> 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.

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:

Language: Python Syntax
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.

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.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

You must own this product to join the conversation.