A crucial concept in Python programming is the Python function. Let’s define what a function is, exactly:
- A Python function is a named section of a program that performs a specific task and, optionally, returns a value
Functions are the real building blocks of any programming language.
Table of contents
Advantages of using functions
A Python function can be defined once and used many times. It’s a great way to keep your code short, concise, and readable. By giving a function a well-chosen name, your code will become even more readable because the function name directly explains what will happen. This way, others (or future you) can read your code, and without looking at all of it, understand what it’s doing anyway because of the well-chosen function names.
Functions accept a parameter, and you’ll see how this works in a moment. The big advantage here, is that you can alter the behavior of the function by changing the parameter.
A function can return a value. This value is often the result of some calculation or operation. In fact, a Python function can return multiple values.
Built-in Python functions
Before we start defining functions ourselves, we’ll look at some of Python’s built-in functions. Let’s start with the most well known built-in function, called
>>> print('Hello, readers!') Hello, readers! >>> print(15) 15
Print takes an argument and prints it to the screen.
As stated in our definition, functions can optionally return a value. However,
>>> result = print('Hello') Hello >>> print(result) None
None is a special type of value in Python, basically meaning ‘nothing.’
Another built-in function, that does return a value, is
len(). It returns the length of whatever you feed it:
>>> mylength = len('Hello') >>> print(mylength) 5
Creating a Python function
Now that we know how to use a function, let’s create one ourselves:
>>> def say_hi(): ... print('Hi!') ... >>> say_hi() Hi!
It’s just a few lines, but a lot is going on. Let’s dissect this:
- First of all, we see the word
def, which is the Python keyword used to define a function.
- Next comes our function name, say_hi.
- Then we encounter two parentheses, (), which indicate that this function does not accept any parameters (unlike
- We end the line with a colon (:)
- And finally, we bump into a feature that sets Python apart from many other programming languages: indentation.
Python uses indentation to distinguish blocks of code that belong together. Consecutive lines with equal indentation are part of the same block of code.
To tell Python that the following lines are the body of our function, we need to indent them. You indent lines with the TAB key on your keyboard. In Python, it’s good style to use four spaces for indentation. The entire Python community does so. The Python interactive shell (REPL) and all decent editors will automatically indent with four spaces if you hit the TAB key.
Back to our function: it has only one line of code in its body, the print command. After it, we hit enter one extra time to let the Python REPL know that this is the end of the function. Finally, we can call our function with
Python function parameters and arguments
We can make this more interesting by allowing an argument to be passed to our function:
>>> def say_hi(name): ... print('Hi', name) ... >>> say_hi('Erik') Hi Erik
Our function now accepts a value, which we assign to the variable name. We call such variables the parameter, while the actual value we provide (‘Erik’) is called the argument.
- Parameters and arguments
- A Python function can have parameters. The values we pass through these parameters are called arguments.
As you can see,
print() accepts multiple arguments, separated by a comma. This allows us to print both ‘hi’ and the provided name. For our convenience,
print() automatically puts a space between the two strings.
name only exists inside our function. We say that the scope of the variable
name is limited to the function
say_hi, meaning it doesn’t exist outside of this function.
- The visibility of a variable is called scope. The scope defines which parts of your program can see and use a variable.
If we define a variable at the so-called top-level of a program, it is visible in all places.
Let’s demonstrate this:
>>> def say_hi(): ... print("Hi", name) ... answer = "Hi" ... >>> name = 'Erik' >>> say_hi() Hi Erik >>> print(answer) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'answer' is not defined
say_hi was able to use the variable name, as expected, because it’s a top-level variable that is visible everywhere. However, answer, defined inside say_hi, is not known outside of the function and causes a
NameError. Python gives us an informative and detailed error: “name ‘answer’ is not defined.”
Python function with multiple arguments
Let’s make this even wilder and define a function with multiple arguments:
>>> def welcome(name, location): ... print("Hi", name, "welcome to", location) ... >>> welcome('erik', 'this tutorial') Hi erik welcome to this tutorial
It’s not that hard, you just add more arguments to the function definition, separated by commas.
Default values and named parameters
A compelling Python feature is the ability to provide default values for the parameters:
>>> def welcome(name='learner', location='this tutorial'): ... print("Hi", name, "welcome to", location) ... >>> welcome() Hi learner welcome to this tutorial >>> welcome(name='John') Hi John welcome to this tutorial >>> welcome(location='this epic tutorial') Hi learner welcome to this epic tutorial >>> welcome(name='John', location='this epic tutorial') Hi John welcome to this epic tutorial
Because our parameters have a default value, you don’t have to fill them in. If you don’t, the default is used. If you do, you override the default with your own value.
Calling Python functions while explicitly naming the parameters is different from what we did up until now. These parameters are called named parameters because we specify both the name and the value. Thanks to these named parameters, the order in which we supply them doesn’t matter. If you think about it, it’s the natural and only way to make default values useful.
If you don’t want to use named parameters, you can. When you rely on position instead of names, you’re providing what we call positional parameters. The position matters, as can be seen below:
>>> def welcome(name='learner', location='this tutorial'): ... print("Hi", name, "welcome to", location) ... >>> welcome('Erik', 'your home') Hi Erik welcome to your home
For now, you’ve learned enough about functions to continue with the guide. If you like to learn more though, make sure to read our Python functions deep dive.