Python Functions, Explained.

Here is a more intuitive way of understanding Python functions.

Brunna Torino
Towards Data Science

--

by brunnatorino via Canva

What are functions?

We first learn about functions in high school mathematics. The general concept of functions in maths is that there is an input (let’s call it x) and an output (let’s call it y). We represent a mathematical function like this:

y = f(x)

where f(x) can be any transformation (such as adding, subtracting, multiplication, exponentials…) to the input we call x. One example could be:

y = 2x + 1

where the f represents the equation where we will take the output, multiply it by two and add one, and most importantly, we want to return it as y.

Why am I going back this far? Programming languages will always use fundamental mathematics concepts to build their structure and logic. If you understand what functions are in mathematics, you already understand what functions are in Python (and other programming languages too).

How do we represent functions in Python?

Let’s use the mathematical explanation above to help us understand how to write functions. Try to think about coding as a conversation with the computer, and Python as a means to express your commands step-by-step.

First, we need to define the name of the function, and the input x (just like our mathematical example). We write def from define, the function name (which you could think about as f in mathematics), and the input variable x. We call x the parameter of the function.

def my_function_name(x):

In maths, writing only f(x) will transform x but won’t assign the value of x to any variable that we can refer to later on. So we need to add a return statement to the end of the function:

def my_function_name(x):

return y

We are using x and returning y, but what is the connection between the two? How do we calculate y?

If you think about coding as explaining your commands step-by-step, it makes sense for the function calculations to go after defining the function but before returning the value of y.

def my_function_name(x):
y = 2*x + 1
return y

Are we done? Not yet!

The difference between defining a function and writing directly:

y = 2*x + 1
print(y)

is that we can save functions if we want to use them later, with different values of x. If we want to use it the function in our script, we need to call it into action:

variable1 = my_function_name(4)

Notice anything different here? We are using 4 instead of x when we call the function. In fact, we want x to be equal to 4 at this time. We call 4 the argument of the function. The beauty of functions is that we don’t ever need to define globally what x is: x can take any value we write in between the parentheses when we call the function.

What about variable1? The same concept explained with x applies here: in this one time we are calling the function, we want variable1 to be y. This means that whatever y from our function turns out to be (2*4 + 1 = 9), we want that value to be assigned to variable1.

You can go on to use variable1 in the rest of your script just as if you had written this:

variable1 = 9

Cool, what else can I do with functions?

Going back to mathematics, let’s say you have a function with two inputs:

y = f(x,y)

You can do the same with Python functions, in the exact same way as maths:

def my_function_name(x,y):
y = x + y
return y

When calling functions, you need to be consistent with the number of inputs you initially used when defining it:

variable2 = my_function_name(1,2)

You can also not specify the number of arguments by writing *arg as the parameter when defining your function. This is where it differs a bit from mathematics: you can call as many arguments as you want to be used in the function. Here, we are creating a list and append all the numbers in the function arguments to the list.

def my_function_name(*arg):
list_of_numbers = []
list_of_numbers.append(arg)
return list_of_numbers

For example, if we called the function like this:

my_function_name(1,2,3,4,5,6,7,8,9)

you can expect this output:

Out: [1,2,3,4,5,6,7,8,9]

Example: Matrix Calculator

To illustrate how we would use functions in a more advanced topic, let’s go through an example of how to build a matrix calculator that will return the determinant of 2x2 matrices.

Here, I am defining the function name as matrix_calculator and passing 4 inputs (also known as parameters): x1,x2,y1 and y2 that are used to create the matrix called my_matrix. Next, I calculate the determinant and return it.

If I call the function with the arguments 1, 2, 3 and 4:

matrix_calculator(1,2,3,4)

I will get this output:

-2

Let’s make the calculator more advanced. What if the function could understand if you wanted to calculate the determinant of 2x2 or a 3x3 matrix depending on how many arguments you call the function with?

We will the numpy function np.linalg.det(A) to calculate the determinant, so we will start by importing the numpy package. Next, I will define the function called smart_matrix_calculator and will pass to the function as many arguments as I want by using *arg as the parameter. Then I will transform the numbers into a list, so that we can slice the list and create the matrix.

If the length of the numbers we passed is 9 (if len(arg) == 9) that means we want a 3x3 matrix that has 9 elements.

We transform it into a matrix by slicing the list in the following way:

  • first three numbers will be x1, y1 and z1 respectively
  • the next three numbers will be x2, y2 and z2
  • the last three numbers will be x3, y3 and z3

We transform it into an array in the same line, calculate the determinant, and finally return it at the end of the function.

If we call the function using the numbers (1,2,3,4,1,2,2,1,9) to produce a 3x3 matrix:

smart_matrix_calculator(1,2,3,4,1,2,2,1,9)

We can expect it to return:

Out: -51.0

If we call the function using the numbers (1,2,3,4) to produce a 2x2 matrix:

smart_matrix_calculator(1,2,3,4)

We can expect it to return:

-2.0

Thank you for reading. I hope my way of understanding what functions are and how they worked helped you to understand a bit more about this essential programming skill.

Let me know in the comments if you would like more tutorials about other Python topics!

--

--