Functional Programming
In every single functional programming article you're gonna see that it says in functional programming we treat functions as a first-class citizens to the point
absolutely 100% accurate but sometimes these phrases get so much popular that you start wondering what it actually means can somebody tell me in a plain
simple language?
What is Functional programming:
In this article I'm gonna explain you what functional programming is and I promise you after reading this article it's gonna be absolutely simple and absolutely breeze to understand what functional programming is. Now before we move on to understand the functional programming I got a question for you as of now currently in whatever the programming language you are writing your code let me know in the comment section are you using procedural way of writing programs or you are using object-oriented way or you're already using functional programming??. Another thing is that functional programming is absolutely amazing and it's mostly independent of programming language but support of functional programming is now very gentle in some of the programming language only some are supporting it. Some are releasing updates to support it and eventually in future many more programming language will come up and we'll support functional programming functional programming it's not a new jargon it just is a way a style of writing programming and treating some values and some function in a bit different way than we used to treat it in object-oriented programming.
Now definitely there are a variety of definitions in every single programmer can debate in the world of functional programming whether this is functional or this is not but there are three core important things on which every single programmer is going to agree that. Yes these are the part of functional programming and I can explain you them in a much easier way on a code editor so let's move.
Four Cores:
There so there are four core important things that you absolutely need to know the first one is that functional programming is more aware of writing style of code so it's majorly independent of programming language but there are some programming language which is supporting it's - it's at its best some are not so there are a couple of concepts that you need to understand. Let me expand this this is just simply is code and I'm not gonna be running any code I'm gonna just explain these syntax using the help of code so. Let's just say we got a score and you are playing on to some great level you got a score of 450. Now on that let's just say you have a function again this is not really a core function that just the gist of how function usually looks like so we're gonna simply say that at this exact stage you have done something so that we want to add a bonus appear what you can do up here you can simply call this up here simply say score is gonna be equals to score plus and you just add some number like 50 and finally you just go ahead and return score and don't yell at me by saying hey you should say this dot score. It's more over 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 gonna notice that these things don't happen moreover a functional if I write this into a functional programming way I would write it in a bit different manner I would write the same function like this. I'm gonna simply say that hey this is my
function again add a bonus and this is gonna take a parameter score and what we're gonna do now here we are gonna simply just hit return and we're gonna simply say whatever the bonus or whatever the score we have got we just want to add let's just say 45 on to add. Now there's a huge difference between this piece of code and this piece of code now it's not much shorter you can also make it a shorter like this but that's not the point in the functional programming we keep our functions and data totally separate so that's the step number one or kind of a unspoken rule in the functional way so that keep your data and keep your functional totally separate while debugging this piece of code is much easier to debug because it is not referring to any global variable which might be causing the issue this is moreover like self intake function which just runs on its own and it's also not much 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 gonna add 50 to it so that's the basic gist of it so that's the one thing keep your data and your functions totally separate so that's one step.

Now again one another thing is actually state change so this is state change is if I can write that this is state change is a very interesting thing and it's a bit difficult to understand for beginners but I'm gonna try my best these examples will just simplify like ridiculous level. So it says that you should not change the state much often as much as you can avoid it just do it for example I am iteration I say let's just say hey a lot which is true and then later on I realized that this variable should not say hey it should say something like hey there and then I realize the same variable should say something like this hey there and then everyone so there is nothing wrong in 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 this one so in functional programming style. We actually don't reuse this variable much often we don't change it much often in fact what we do is let's just say hey page says hey and then we want to update the somehow so we're gonna say that H on have redness day says hey there if I can write it hey there and hit H on a Saturday says hey there again come on hey there everyone so this is more like a functional style you don't update the same variable in fact. It is okay to really clear or kind of a newly declare again available with some more
modification so that it's much more clear and it's okay for us so this thing is pretty common of the don't change your state and mutability immutability. This is common thing in the function
programming again the rule of thumb is that make sure you don't really update the same variable again and again you instead declare more of them.
Do you want to know the last part of it?
Now coming up on to the last part which is very funny and pretty interesting as well so in this what they say is something like this function or functions are treated as first class okay this is a bit of a bit difficult statement to understand for beginners but I'm gonna make it absolutely ridiculously simple. So let's just say we have a Const with H and it is storing 50 I don't know why I'm always using 50 but that's it and we also have got a variable which is named in your language however you define it that's okay and if I just say this variable is it age that's okay but what functional programming says that the way you are treating your numbers and string and you are throwing them around anywhere like you can declare them in a variable you can pass them in a function 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 add score something like that score and that can also be a simple function whatever you want to do inside that that should be the case I want to men want to say this so this is how the function should look like in a functional programming it can be it should be able to store functions into any variable and not only that if let's just say you have a method which say say hello and this is our method in here also you should be able to accept function as a parameter so I can simply say function and then simply say like this whatever you want to say or maybe a reference would be a better example but this actually makes things simple and easy and here also you can come up and simply say hey return and then we can say function and like this da-da-da-da-da. So this is it we can store the function into available we can pass it as a parameter we can return it as whatever the data type it is so this is the gist of a functional programming.
So these are the three important thing Now the fourth and the most important thing and the fourth most important thing is you need to hit that subscribe button so that I can keep on coming up with these amazing article and you can get notification for all of them. I hope you have enjoyed and that's it for this one I'm gonna catch you up in the next article.
0 Comments