Code With Functional Programming
00:00 Welcome to this lesson, where you’ll explore the fascinating world of functional programming and pure functions. As the name suggests, functional programming relies on functions to perform tasks, but what sets it apart from other programming paradigms is the use of pure functions. Pure functions are functions that don’t change the program’s state.
00:23 This means that when you call the function with a specific input, it always returns the same output. It doesn’t modify any variable outside of its scope or depend on any external state. Now, you might be wondering, why is this important? Well, pure functions make your code more predictable, testable, and easier to reason about. When you use pure functions, you can be confident that your program will behave consistently.
00:49
Python provides functional language features, such as anonymous functions, map()
, filter()
, and reduce()
. You might be wondering what anonymous functions are.
00:59
They’re basically nameless functions that you use when you need a function, but not for a very long time. You just need them for a short period of time. They’re also called lambda functions, and map()
and reduce()
are also functions that are really important.
01:16 In the next lessons, you’re going to dive deeper into all three concepts and learn all about them.
01:25 Let’s explore pure functions and how they work in practice. In this example, you have two functions. One is a pure function, and the other is not.
01:35
The function on the right is not a pure function because it uses a global variable named total
that is set to 0
. When the add_numbers()
function is called with two variables, a
and b
, it adds them together, updates the global total
variable, and then returns it. Right here, because the function modifies the global state—and by that, I mean the value of the total
variable—calling it twice with the same input won’t always produce the same result. The output will depend on the current state of the total
variable.
02:10
The first time the add_numbers()
function runs, the total
variable is initialized to 0
.
02:16
After adding 1
and 2
together, the function updates total
to 3
and returns it as the result.
02:23
On the second iteration, the add_numbers()
function adds the sum of 1
and 2
to the current value of the total
variable, which is 3
from the previous iteration, resulting in a return value of 6
. In conclusion, the add_numbers()
function you just explored is not a pure function because it modifies the global state.
02:45 You might be wondering, if this isn’t functional programming, then what is it? The answer is imperative programming, which is the opposite of functional programming.
02:55
You’ll hear more about this shortly. On the other hand, the function on the left is a pure function, since it does not modify any global variable and always returns the same output for identical inputs. As you can see, the first time you run the function with inputs 1
and 2
, it returns 3
.
03:15
And when you run it again, it also returns 3
. This is great because you can rely on this function to always produce the same output for the same inputs.
03:28 In the previous slide, you briefly learned about imperative programming, which is the opposite of functional programming. To conclude everything you just learned about, imperative programming changes the program’s state, and functional programming doesn’t.
03:42 You’ll see anonymous functions, a.k.a. lambda functions, being used a few times in this course, so let’s explore them in the next lesson.
Become a Member to join the conversation.