I’m a mid level Swift developer and I’ve only ever worked with the Object Orientated Programming paradigm.
I’ve heard about functional programming and I tried Googling what it is, but I couldn’t understand the resources that explained it. Also, reading functional code is even more confusing.
So I was hoping that someone who understood it could break it down in incredibly basic and simple terms, and the benefits/drawbacks that it has compared to OOP.
In: Engineering
Im not sure where to start for you.
But lets start with pure functions. These are functions that have two basic properties. They always give the same output for the same input, this is in contrast to OOP where the output depends on the object even with the same input. Second property they dont modify any variables or arguements or anything else ie. they dont have side effects. This is also basic property of functional programming as a whole once you intialize a variable you cant change the variable. If you want to use another value you have to create a new variable. Another thing is you dont have while or for loops you do loops by recursion. This also what makes it probably hard to read for you.
And for the benefits/drawbacks copied from [https://www.geeksforgeeks.org/functional-programming-paradigm/](https://www.geeksforgeeks.org/functional-programming-paradigm/) :
**Advantages:**
1. Pure functions are easier to understand because they don’t change any states and depend only on the input given to them. Whatever output they produce is the return value they give. Their function signature gives all the information about them i.e. their return type and their arguments.
2. The ability of functional programming languages to treat functions as values and pass them to functions as parameters make the code more readable and easily understandable.
3. Testing and debugging is easier. Since pure functions take only arguments and produce output, they don’t produce any changes don’t take input or produce some hidden output. They use immutable values, so it becomes easier to check some problems in programs written uses pure functions.
4. It is used to implement concurrency/parallelism because pure functions don’t change variables or any other data outside of it.
5. It adopts lazy evaluation which avoids repeated evaluation because the value is evaluated and stored only when it is needed.
**Disadvantages:**
1. Sometimes writing pure functions can reduce the readability of code.
2. Writing programs in recursive style instead of using loops can be bit intimidating.
3. Writing pure functions are easy but combining them with the rest of the application and I/O operations is a difficult task.
4. Immutable values and recursion can lead to decrease in performance.
Latest Answers