TNS
VOXPOP
Where are you using WebAssembly?
Wasm promises to let developers build once and run anywhere. Are you using it yet?
At work, for production apps
0%
At work, but not for production apps
0%
I don’t use WebAssembly but expect to when the technology matures
0%
I have no plans to use WebAssembly
0%
No plans and I get mad whenever I see the buzzword
0%
Linux / Python / Software Development

How to Define and Use Your Own Functions in Python

Learn how to define and use your 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.
Jan 24th, 2024 7:06am by
Featued image for: 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:


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:


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:


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:


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:


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:


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:


Next, we’ll define the current date/time as x like so:


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:


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.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.