What is Functional Programming?
Functional programming (also known as FP) is a way of thinking about software development that involves the creation of pure functions. It avoid concepts of shared state, mutable data observed in Object Oriented Programming.
Functional languages emphasis on expressions and declarations rather than execution of statements. Therefore, unlike other procedures which depend on a local or global state, value output in FP depends only on the arguments passed to the function.
Characteristics of Functional Programming
â˘Functional programming method focuses on results, not the process
â˘Emphasis is on what is to be computed
â˘Data is immutable
â˘Functional programming Decompose the problem into âfunctions
â˘It is built on the concept of mathematical functions which uses conditional expressions and recursion to do perform the calculation
â˘It does not support iteration like loop statements and conditional statements like If-Else
History of Functional programming
â˘The foundation for Functional Programming is Lambda Calculus. It was developed in the 1930s for the functional application, definition, and recursion
â˘LISP was the first functional programming language. McCarthy designed it in 1960
â˘In the late 70âs researchers at the University of Edinburgh defined the ML(Meta Language)
â˘In the early 80âs Hope language adds algebraic data types for recursion and equational reasoning
â˘In the year 2004 Innovation of Functional language âScala.â
Functional Programming Languages
The objective of any FP language is to mimic the mathematical functions. However, the basic process of computation is different in functional programming.
Here, are some most prominent Functional programming languages:
â˘Haskell
â˘SML
â˘Clojure
â˘Scala
â˘Erlang
â˘Clean
â˘F#
â˘ML/OCaml Lisp / Scheme
â˘XSLT
â˘SQL
â˘Mathematica
Basic Functional Programming Terminology and Concepts
Immutable Data
Immutable Data means that you should easily able to create data structures instead of modifying ones which is already exist.
Referential transparency
Functional programs should perform operations just like as if it is for the first time. So, you will know what may or may not have happened during the programâs execution, and its side effects. In FP term it is called Referential transparency.
Modularity
Modular design increases productivity. Small modules can be coded quickly and have a greater chance of re-use which surely leads to faster development of programs. Apart from it, the modules can be tested separately which helps you to reduce the time spent on unit testing and debugging.
Maintainability
Maintainability is a simple term which means FP programming is easier to maintain as you donât need to worry about accidentally changing anything outside the given function.
First-class function
âFirst-class functionâ is a definition, attributed to programming language entities that have no restriction on their use. Therefore, first-class functions can appear anywhere in the program.
Closure
The closure is an inner function which can access variables of parent functionâs, even after the parent function has executed.
Higher-order functions
Higher-order functions either take other functions as arguments or return them as results.
Higher-order functions allow partial applications or currying. This technique applies a function to its arguments one at a time, as each application returning a new function which accepts the next argument.
Pure function
A âPure functionâ is a function whose inputs are declared as inputs and none of them should be hidden. The outputs are also declared as outputs.
Pure functions act on their parameters. It is not efficient if not returning anything. Moreover, it offers the same output for the given parameters
Impure functions
Impure functions exactly in the opposite of pure. They have hidden inputs or output; it is called impure. Impure functions cannot be used or tested in isolation as they have dependencies.
Function Composition
Function composition is combining 2 or more functions to make a new one.
Shared States
Shared states is an importance concept in OOP programming. Basically, Itâs adding properties to objects. For example, if a Hard Disk is an Object, Storage Capacity and Disk Size can be added as properties.
Side Effects
Side effects are any state changes which occur outside of a called function. The biggest goal of any FP programming language is to minimize side effects, by separating them from the rest of the software code. In FP programming It is vital to take away side effects from the rest of your programming logic.
The benefits of functional programming
â˘Allows you to avoid confusing problems and errors in the code
â˘Easier to test and execute Unit testing and debug FP Code.
â˘Parallel processing and concurrency
â˘Hot code deployment and fault tolerance
â˘Offers better modularity with a shorter code
â˘Increased productivity of the developer
â˘Supports Nested Functions
â˘Functional Constructs like Lazy Map & Lists, etc.
â˘Allows effective use of Lambda Calculus
Limitations of Functional Programming
â˘Functional programming paradigm is not easy, so it is difficult to understand for the beginner
â˘Hard to maintain as many objects evolve during the coding
â˘Needs lots of mocking and extensive environmental setup
â˘Re-use is very complicated and needs constantly refactoring
â˘Objects may not represent the problem correctly
Functional Programming
â˘FP uses Immutable data
â˘Follows Declarative Programming based Model
â˘What it focuses is on: âWhat you are doing. in the programmeâ
â˘Supports Parallel Programming
â˘Its functions have no-side effects
â˘Flow Control is performed using function calls & function calls with recursion
â˘Execution order of statements is not very important
â˘Supports both âAbstraction over Dataâ and âAbstraction over Behaviorâ
OOP
â˘OOP uses Mutable data
â˘Follows Imperative Programming Model
â˘What it focuses is on âHow you are doing your programmingâ
â˘No supports for Parallel Programming
â˘Method can produce many side effects
â˘Flow control process is conducted using loops and conditional statements
â˘Execution order of statements is important
â˘Supports only âAbstraction over Dataâ
Conclusion
â˘Functional programming or FP is a way of thinking about software construction based on some fundamental defining principles
â˘Functional programming concepts focuses on results, not the process
â˘The objective of any FP language is to mimic the mathematical functions
â˘Some most prominent Functional programming languages: 1)Haskell 2)SM 3) Clojure 4) Scala 5) Erlang 6) Clean
â˘A âPure functionâ is a function whose inputs are declared as inputs and none of them should be hidden. The outputs are also declared as outputs.
â˘Immutable Data means that you should easily able to create data structures instead of modifying ones which is already exist
â˘Allows you to avoid confusing problems and errors in the code
â˘Functional code is not easy, so it is difficult to understand for the beginner
â˘FP uses Immutable data while OOP uses Mutable data
â