How to Define and Use Your Own Functions in Python

A function is a reusable bit of code that can execute a specific functionality any time it is called within a program. Functions are a great way to make your code more efficient and also to create blocks of code that can be used from one program to another (as long as they fit the purpose of the app in question).
Of course, Python has several built-in functions, such as print(), zip(), map(), eval(), float() and many more. But what happens when you can’t find the function that specifically fits your needs?
You create it.
And you use it.
Again and again.
You might even share it.
If that sounds like something you might want to learn how to do, keep reading to find out.
The Basic Syntax of a Function
The basic syntax of a Python function is quite simple. Consider this:
Let’s start with the usual Hello, World function. We’ll call the function helloWorld(). To create a function, you must use the def keyword, which declares a function. So, the first line of our helloWorld function would be:
1 |
def helloWorld(): |
What we’ve done is instruct Python we are creating a function named helloWorld.
Next, we create the section of code that does something. Since this is a simple Hello, World function, the line for this would be:
1 |
print("Hello, New Stack!") |
Our entire function would look like this:
Great. We’ve created our first function. But what do we do with it? Let me first show you what happens when you create a new Python app with only your defined function. Create a file with your default text editor (I use nano on Linux), named function.py. In that new file, add your function and save it. Next, run the script with:
1 |
python3 function.py |
You should see nothing in the output. Why? Because, although we defined the function, we never used it. To use a function, you have to call it. How do you do that? Simple. Open the file back up and add the following line at the bottom:
1 |
helloWorld() |
That’s it. You’ve effectively called the function you created previously and run it from that line. Now, when you run the app, you should see the following output:
1 |
Hello, New Stack! |
Let’s create a more complicated function, one that will take input from a user for our Hello World output. We’ll define two variables: greeting and name, like so:
Instead of placing what to print in quotes, we’ll instruct Python to print what was inputted for the variables, like so:
1 |
print(greeting, name) |
Our full function looks like this:
You can also define multiple functions. Let’s add a function that asks the user to input their age. That function will look like this:
So, our two functions (in our app) look like this:
We then call the functions with:
Our entire app looks like this:
When you run the app, you’ll be asked to enter the greeting and your name, at which point Python will print the output of that function. Next, it will ask you your age and then print out You are X years old (where X is the number inputted).
Here’s the neat thing about this. You can call the function as often as you need, and you don’t have to call it from the bottom of the app. Let’s say you want to add the printing of the exact date and time when the app is run. For that, we must first import the date/time library like this:
1 |
import datetime |
Next, we’ll define the current date/time as x like so:
1 |
x = datetime.datetime.now() |
Then we’ll add our custom functions as we’ve already done:
Before we call our functions, let’s print out x (which is the current date and time) with the line:
1 |
print(x) |
Finally, we call our functions as per normal:
Our entire script looks like this:
When we run the app, it will first print out the exact date and time, then ask for input for the greeting, followed by input for a name. It will then print out the greeting/name. Next, the app will ask for the user’s age and then it will print out what was inputted.
And that’s how easy it is to create your own functions in Python. These examples may be simplistic, but they make it easy to understand how this works. You can now start to get creative with your functions and call them at will.