Get startedGet started for free

Functional programming in action

1. Functional programming in action

Now it's time to take a look at functional programming in action. We will dive into some specific examples of pure functions in Python and highlight how these differ from general Python functions.

2. Functional programming in action

We're going to be looking at three examples of Python functions that can be written as pure functions and therefore used in a functional paradigm. We will be paying close attention to the key differences between pure functions and Python functions more generally, and specifically the fact that pure functions have no side effects. We'll also demonstrate how pure functions can call other pure functions and remain pure functions. They only become impure when those other functions are impure.

3. Example 1 - Writing a pure function

In this first example, we've written a pure function to square every item in a list. Keeping in mind that we need to consider only the inputs to the function and produce only outputs, no side effects, we approach this problem by first creating a new, empty list to contain our output. Next, we go through each item in the input list, square that value individually, and append it to the new list. When we have finished, we have a new list containing the squared value of every item in the original list, and we return this new list.

4. Example 2 - Correcting an "impure" function

In this second example, we are going to do something a little more complex in the body of our function. We are going to scale the values in the input list based on two additional values: a sample mean and a scaling factor. Within the body of the function, we take each item in the list, subtract off the sample_mean and then divide the result by the scale_factor. But wait! See how this function depends on variables that are defined outside of the function body? That means that this is not a pure function, since it doesn't depend on only the input values to determine the output. Let's see if we can correct this on the next slide.

5. Example 2 - "Impure" function corrected

Now we've made an adjustment to the function from the previous slide. See how we have moved the variables sample_mean and scale_factor from being defined outside of the function to being inputs to the function itself? Now we have ensured that this function is "pure" -- namely that it doesn't depend on anything outside of the inputs to the function itself.

6. Example 3 - Combining pure functions

Building on the previous example, we can also call functions from within other functions. In this example, we've pulled out the logic for scaling each value in the list into a separate function called scale_value. We can then call this function from within the scale_list function. This combining of functions is what allows us to build more complex programs even within the context of only writing pure functions.

7. Let's practice!

That was a lot of examples of pure functions to cover! Let's try writing some of our own in the exercises.