Affiliate links on Android Authority may earn us a commission.Learn more.

How to define a function in Python

July 06, 2025

Learning how to define a function in Python is one of the most important steps to mastering the language. Functions are blocks of code that perform a specific task and can be “called” from any point in the rest of your program. This allows you to avoid writing out large amounts of code over and over, and it lets you handle dynamic processes that react to the context and user interaction.

Let’s take a closer look!

The good news is that Python makes it very simple to define functions. That’s because Python uses a very nice syntax that looks extremely similar to English. To define a function, we simply use the statement “def.” Can you guess what that’s short for?

We then follow that statement with the name of our function (usually capitalizing each word), and then closed brackets. Finally, we use a colon and an indentation.

Python Tutorial

Any code that we want to belong to the function, we then indent. We end with the statement “return” which tells Python to jump back to the point in the code it was at before.

Here’s how you put this into action:

How to pass arguments

Once you know how to define a function in Python, the next step is learning how to pass arguments. Arguments are simply variables that you want to pass to your functions to then be manipulated or in some way transformed.

A simple example would be to pass a string into our function so that we can greet the user by name:

Simply name the variable inside the curly brackets when you name the function, and then remember to pass that value when you call it later on!

And there you have it! That is how to define a function in Python: it really is that easy, which is why so many people love Python! If you want to learn a bit more about using functions, check outHow to call a function in Python.

Thank you for being part of our community. Read ourComment Policybefore posting.