Functional programming is a programming paradigm that evaluates as if it were a mathematical function and avoids state and mutable data. Programs are executed by evaluating expressions as opposed to imperative programming which depends on statements to decide what is to be done next. In lay man’s term a functional programs tells what to do and not how to do it.
Features of fuctional programs
Higher order functions of First class functions
Functions are treated like any other values and so can be passed to other functions as arguments or be returned from other functions. One function can be defined and manipulated from within another function. example: SubtractFromList y=map(\x->x-y). map takes a function and a list as arguments and applies function to all elements of list and returns the list of results.
one function is applied to its arguments one at a time and with each application a new fuction is returned that takes the next argument. Higher order functions and currying is helpful in refactoring the code and reducing the amount of repetition.
Sometimes expressions can yield actions that are considered to be side effects of a particular action. Languages that prohibit side effects are considered to be pure. Functional languages are usually pure. Sometimes it maybe beneficial to write impure code such as in I/O state code. But this should be kept to a minimal and functional languages support this. Side effects can be simulated by using monads which provide implicit threading that the compiler does not know. Example: Maintaining bank account balance. These are applicable to tasks that need to be implemented within a state.
Pure functional languages operate usually on immutable data where instead of altering existing values, altered copies are created and the original copy is preserved.
Recursion is heavily used in functional programming as it is the canonical and often the only way to iterate, A feature often used is tail recursion where one function calls another function as its final action.
Functions are not evaluated unless their values are required to evaluate the function call itself. This leads to reduction of overhead arising from evaluating all functions beforehand. Imperative paradigm uses eager evaluation where function arguments are evaluated before invoking. Example print length([2+1,3*2,1/0,5-4]). In imperative programming this code causes the program to crash as 1/0 cannot be evaluated. In functional programming this program crashes only if the length method is called. It runs error free if length is never called which is not the case in imperative paradigm.
Functional programming provides better support for structured programming. It is clean and avoids side effects. Referential transparency is achieved. The program itself is shorter and easier to write. The use of higher order functions enables better embedding of functionality. However the disadvantage is that running of functional programs is less efficient in terms of CPU and memory use. Functional programming is not very suitable for implementation of I/O operations.