What Is Functional Programming? | The Easy Way
In this blog, we will learn about functional programming in the easiest way. You will learn the definition and examples of functional programming.
Join the DZone community and get the full member experience.Join For Free
Hey everyone, I am Ryan Roy. In this blog, I’m going to explain what functional programming is and I promise you that after watching this blog, it’s going to be absolutely simple and a breeze to understand what functional programming is.
In every single functional programming blog, you’re going to see that it says in functional programming, we treat functions as first-class citizens. To the point, it is absolutely 100% accurate. Sometimes, these phrases get so much popularity that you start wondering what it means and whether someone can tell you in plain, simple language.
Now, before we move on to understand functional programming, I have a question for you. As of currently, in whatever programming language you are writing your code, let me know through the blog comment section: are you using a procedural way of writing programs or using an object-oriented way, or already using functional programming?
Functional Programming vs OOP
Let’s understand this using an informative infographic that you can find at the link below:
What Is Functional Programming?
Now, the thing is that functional programming is absolutely amazing and it’s mostly independent of programming language. But, the support of functional programming is now very gentle in some of the programming languages only. Some are supporting them, some are releasing updates to support it, and, eventually, in the future, many more programming languages will come up and will support functional programming.
Functional programming is not a new jargon it just is a way or a style of writing programming and treating some values & function in a bit different way than we used to treat them in object-oriented programming.
There are a variety of definitions in every programmer that can debate in the world of functional programming, whether this is functional or this is not. But, there are three core important things in which every single programmer is going to agree that, yes, these are part of functional programming. I can explain them in a much easier way on a code editor, so let’s move to that.
Four Core Things You Need to Know
The first & important one is that functional programming is more of writing “style of code,” so it’s majorly independent of programming language. But, there are some programming languages, which is supporting it at its best some are not.
So, there are a couple of concepts that you need to understand let me expand this. This simply is code, and I’m not going to be running any code, I’m going to just explain this syntax using the help of code. So, let’s just say we got a score and you are playing on to some great level, and you got a score of 456.
Now, on that, let’s just say you have a function again this is not really a core function and is just the gist of how a function usually looks. So, we’re going to simply say that you have done something at this exact stage so that adding a bonus appears. What you can do up here is you can simply call this the score that is going to be equal to score + and you just add some number like 45 and, finally, you just go ahead and return the score, as mentioned in the picture, and you can reach that at the link below:
Please don’t yell at me by saying, "Hey, you should say this dot score is more about understanding the concept, not exactly pinpointing the stuff." So, this is all okay, but this is not really much of an acceptable thing in functional programming.
In functional programming, you’re going to notice that these things don’t happen. Moreover, if I write this in a functional programming way, I would write it in a bit of a different manner.
I would write the same function like this, and I’m going to simply say that, "hey this is my function again, add a bonus, and this is going to take a parameter score." We’re going to simply just hit return, and we’re going to simply say that whatever the bonus or whatever the score we have that we want to add, let’s just say 45, on to add.
Now, there’s a huge difference between both the above pieces of code. Now, it’s not much shorter, but you can also make it shorter, but that’s not the point.
In functional programming, we keep our functions and data totally separate, which is step number one. Or, the kind of unspoken rule in a functional way so that you can keep your data and your functional totally separate. Debugging this piece of code is much easier to debug because it does not refer to any global variable that might be causing the issue.
This is more like the self intake function, which just runs on its own. It’s also not very dependent on what kind of data we are passing, whether it’s a score or maybe it’s life. Whatever it is, it’s just going to add 45 to it. So, that’s the basic gist of it; that’s the one thing keep your data and your functions totally separate.
Now, again, one another thing is actually state change, which is a very interesting thing, and it’s a bit difficult to understand for beginners. But, I’m going to try my best and these examples will just simplify on a ridiculous level. So, it says that you should not change the state much often as much as you can avoid it. For example, a person may say “hey” a lot and, then, later on, he realizes that this variable should not say “hey,” it should say something like “hey there” and, then, he realizes the same variable should say something like this: “hey there” and, then, “everyone.” So, there is nothing wrong with this style of code, we are just having a variable, and this variable is getting updated all the time.
Now, keeping the track of this state is actually a bit tricky to understand. So, in functional programming style, we actually don’t reuse this variable much often, and we don’t change it much often. In fact, what we do is that we just say “hey” and the page says “hey” and, then, we want to update them somehow, so we’re going to say that this person on Wednesday says, “Hey there” and, on a Saturday they say, “hey there” again and “hey there everyone.”
So, this is more like a functional style, and you don’t update the same variable. It is okay to really clear or newly declare again available with some more modification so that it’s much clearer and okay for us. So, this thing is pretty common: don’t change your state and mutability and immutability, which is common in functional programming. Again, the rule of thumb is to make sure you don’t really update the same variable repeatedly. You, instead, declare more of them. Check out the image at the link below:
I am now coming up with the last part, which is very funny and pretty impressive as well. So, in this, what they say is something like "this function or functions are treated as first-class." This is a bit of a difficult statement to understand for beginners who are into software development. But, I’m going to make it absolutely, ridiculously simple.
So, let’s just say we have a “Const with H” and it is storing 45. And, we also have got a variable which is named in your language. However, you define it, and that’s okay. If I just say this variable is “Hitesh” (the person), then that’s okay!
But, what functional programming says is that the way you are treating your numbers and string and throwing them around, you can declare them in a variable and you can pass them in a function. Also, you can return them in a function. The same kind of treatment should be given to the functions as well. So, we can simply say “Const addscore” or something like that score and that can also be a simple function. Whatever you want to do inside that, it should be the case.
So, this is how the function should look in functional programming. It should be able to store functions into any variable and not only that, let’s just say that you have a method, which is “say hello” and this is our method. Also, you should be able to accept a function as a parameter. So, I can simply say function and, then, simply say whatever you want to say or maybe a reference would be a better example, but this actually makes things simple and easy. Look at the image in the link below:
So, this is it; we can store the function into available, we can pass it as a parameter, and we can return it as whatever the data type it is. So, this is the gist of functional programming, which are the three important things.
Let’s Wrap Up About Functional Programming
I hope this blog helps you understand what functional programming is in an easy way. If you have a query or want to add anything, please write it in the comment box. Let’s discuss a bit more about functional programming in the comment box below!
Opinions expressed by DZone contributors are their own.