Writing functions in Python


Learn how to write functions in Python

26 May, 2021 · 2 min read

Functions are an essential part of programming, as they can execute a block of code at once. Often it’s an excellent way to re-use blocks of code.

Let’s give it a go and see how they work in Python.

Creating a function in Python

A function is made by prefixing the def keyword.

def foo():

However, running our code now will not do anything since we didn’t call our function yet.

Executing the function

To run/execute the function, we must call it somewhere in our code. In general, this happens when a specific criterion is matched, but let’s just run it as is.

def foo():


When we run our code now, it returns Bar

Passing and returning data

Having a function that prints out something is not convenient so let’s see how we can give it data and return something.

Let’s say we want to make a function that multiplies a number by itself and returns the output.

Meaning, if we put in the number 5, it should run 5x5 and return 25.

def multiply(number):
    return number * number


Running this code will indeed return 25.

We can easily have the function accept multiple arguments like so:

def multiply(number, multiplier):
    return number * multiplier

print(multiply(5, 10))

This will return 50.

And one cool thing we can do is send the arguments based on their key value like this.

multiply(number=5, multiplier=2)

You might not know how many arguments you’re expecting in some cases, and you can prepend the argument with an asterisk (*).

def feed_animals(*animals):

feed_animals('Cow', 'Chicken', 'Goat')

Which will return:

('Cow', 'Chicken', 'Goat')

There are some more additions to functions, but this covers the basics for Python functions.

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Speedtest your connection in Python

21 Jun, 2021 · 2 min read

Speedtest your connection in Python

F-strings in Python

7 Jun, 2021 · 2 min read

F-strings in Python

Join 2099 devs and subscribe to my newsletter