Not quite sure how functions work in Python? We've got you covered, here's a guide to get you started.

As with just about every programming language, Python has a concept called functions. At their simplest, functions are groupings of code that perform a certain task. Typically, functions are typically created when a certain task needs to be performed that requires more than one line of code.

Functions are a great way to package up multiple lines of code which then provides the user an easy way to run all of that code at once instead of typing each line, hitting Enter and repeating.

In Python, a function is created by using the def keyword which creates a function definition. The def keyword is an indicator to the Python interpreter that some code immediately following the def keyword will be a function and to treat it like that.

The def keyword is then always followed by the name of the function, an open and close set of parentheses (if no parameters) and a colon which indicates the start of the body of the function. This colon is sometimes replaced with an open curly brace in other languages.

def printhelloword():

We've created our first function! Hit Enter, and it does absolutely nothing.

Let's add some code inside of the function to actually make this effort worthwhile. To build the function's code, we need to indent the next immediate line by at least one space although four spaces or a single tab is common.

To the dismay or some, Python uses whitespace to delineate various constructs in the language. So, for example, if I want my printhelloword() function to print the phrase Hello, world! when it's run, I could indent the next line and add a print() reference.

def printhelloword():
	print('Hello world!')

Once defined, I can then call this function with printhelloworld().

def printhelloword():
    print('Hello world!')

> printhelloword()
Hello world!

However, if I don't include that whitespace before the print() reference, I would get a syntax error so remember to indent!

def printhelloword():
print('Hello world!')
> print('Hello world!')
^  IndentationError: expected an indented block

We've now created our first function but what if I don't always want to print the string Hello world!? Perhaps I'd like to return Hello dolly! instead. In that case, I need to introduce a parameter to my function.

A parameter is a way to provide an input to your function. A parameter allows you to pass different values into your function to then be processed inside of the function. In our case, I want the ability to change the string world with whatever word I want when the function is executed.

To create a parameter, we create a variable inside of the function's parentheses separated by a comma. Since in our case, we just need a single parameter, I'll just add one called word. I then need to replace my static reference to the word "world" and instead replace it with the parameter variable.

To replace the static world reference, notice that I had to create three "groups"; 'Hello', wordand '!'. This is called string concatenation and is one method of inserting a variable inside of a string in Python. The + operator "adds" each string together to form the final string of Hello dolly!.

def printhelloword(word):
	print('Hello ' + word + '!')

Once we've added the parameter, we can then call the function again, but instead of just appending an open and close parentheses after the function name, we can insert the parameter argument. The parameter argument is the value of the parameter at runtime.

> printhelloword('dolly')
Hello dolly!

Now notice that instead of printing out Hello world!, it's printed out Hello dolly!. Our function is now capable of printing out strings like Hello <whatever>.

For more information about creating Python functions, the LearnPython site has a great interactive tutorial that goes over a lot more than what we could cover here.

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!