Functional programming is the method of building software by composing pure functions that are declarative rather than imperative. It avoids the Object-Oriented Programming notions of shared state, mutable data, and side effects.
Functional code tends to be more concise, more predictable, and easier to test than object-oriented code
It is good to note that Functional programming is a programming paradigm.
Examples of Functional programming languages include Haskell, SML, Clojure, Scala, Erlang, F#, SQL, Mathematica.
To grasp what functional programming entails in practice, you must first grasp its fundamental ideas. They might be odd terminology, I would explain them below.
Programming paradigm
The term programming paradigm refers to a style of programming. It does not refer to a specific language, but rather it refers to fundamental, defining principles. Other programming paradigms include object-oriented programming and procedural programming.
Function composition
Function composition is the process of combining two or more functions in order to produce a new function or perform some computation.
Pure function
A pure function is a function that produces the same outputs when given the same inputs and has no side effects.
Pure functions act on their parameters. It is inefficient if nothing is returned.
function Pure(a,b) {
return a+b;
}
Imperative vs Declarative
Declarative programs focus on what has to be accomplished rather than how it should be accomplished. It is more reliant on expressions.
Imperative programs on the other hand focus on flow control, which is the steps used to achieve it. It makes use of statements.
Shared state
A shared state is any variable, object, or memory space that exists in a shared scope, or as the property of an object being passed between scopes. A shared scope can include global scope or closure scopes. Basically, It's adding properties to objects.
When you avoid a shared state, the timing and order of function calls don’t change the result of calling the function.
Mutable data
Mutable data is any data that can be modified after it is created, while immutable data is data that can’t be modified after it’s created. Immutable objects can’t be changed at all.
Side effect
A side effect is an application state change that is observable outside the called function other than its return value. Examples of side effects include: logging into the console, writing to a file, modifying any external variable or object property, etc
Side effects are avoided in functional programming, which makes the effects of its programs much easier to understand, and much easier to test.
I hope you now have a good understanding of Functional Programming.