What is functional programming?
1. What is functional programming?
It's time to learn about a powerful programming paradigm: functional programming! We will cover some basic definitions and concepts first, then dive into examples of functional programming using Python. Let's get started!2. What is functional programming?
So what is functional programming? Functional programming is a style of programming that involves the use of "functions" -- and specifically what are called "pure functions". These should not be confused with the functions in a particular programming language, like Python, but have a specific meaning in functional programming. A pure function is a process that takes inputs, processes them, and produces output based only on these inputs and nothing else. They don't do anything else. Functions are also the mechanism by which functional programming employs separation of responsibilities and allows for modularity.3. What is a pure function?
What exactly is the point of a pure function, and how is it different from any other kind of function? The concept of a pure function is related to the concept of a mathematical function. The rules of a pure function are that it can only look at input values, not anything else about the context in which it's called, and it can only produce output, no other effects. These other effects are referred to as "side effects", so we say that a pure function has "no side effects". Let's look a little closer at what might be considered a "side effect" in this context. A pure function cannot modify any variables outside of the function scope, and it also cannot save any information to an external source like a file or a database.4. Example of a pure function
To make this concept of "pure functions" more concrete, let's look at a quick example in Python. On the left, we have defined a simple Python function called pure_sum that takes two input values and returns their sum. This is a pure function because its output depends only on the input values (x and y), and it does not generate any side effects. On the right, we have defined a second Python function called not_pure_sum. This is not a pure function because, in addition to returning the sum of its input values, it also prints their sum to the screen. Printing the output value to the screen is an example of a side effect, so this is not a pure function. Remember that in functional programming, we only use pure functions. Therefore, only the program on the left could be considered a functional program.5. Benefits of pure functions
We've seen an example of a pure function in Python, and at this point it might be hard to see the benefit of this restriction. Building programs out of only pure functions does however have advantages. One important advantage is that a pure function is easier to understand and debug because we can be sure that it won't have any unintended side effects. Similarly, it is easier to write tests for pure functions than for functions that depend on context beyond their input values. Finally, we can trust that the output of a pure function with given input values will always be the same. This is similar to how a mathematical function is predictable: think of how five squared is always 25, no matter where we are or what day it is. As the paradigm of functional programming relies on the use of pure functions, all of these advantages also apply to using functional programming as a paradigm.6. Let's practice!
Super! We've made it to the end of this introductory video on functional programming and pure functions. Let's test what we've learned with some exercises.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.