Functional programming is a programming paradigm in which we try to bind everything in pure mathematical functions. It is a declarative style. Its main focus is on “what to solve,” in contrast to an imperative style, where the main focus is on “how to solve.” It uses expressions instead of statements. An expression is evaluated to produce a value, whereas a statement is executed to assign variables. Those functions have some special features discussed below.
Lambda calculus is a framework developed by Alonzo Church to study computations with functions. It can be called the smallest programming language in the world. It defines what is computable. Anything that can be computed by lambda calculus is computable. It is equivalent to a Turing machine in its ability to compute. It provides a theoretical framework for describing functions and their evaluation. It forms the basis of almost all current functional programming languages.
Fact: Alan Turing was a student of Alonzo Church who created Turing machine which laid the foundation of imperative programming style.
Programming Languages that support functional programming: Haskell, JavaScript, Python, Scala, Erlang, Lisp, ML, Clojure, OCaml, Common Lisp, Racket.
These functions have two main properties. First, they always produce the same output for same arguments irrespective of anything else.
Secondly, they have no side-effects i.e. they do not modify any arguments or local/global variables or input/output streams.
Later property is called immutability. The pure function’s only result is the value it returns. They are deterministic.
Programs done using functional programming are easy to debug because pure functions have no side effects or hidden I/O. Pure functions also make it easier to write parallel/concurrent applications. When the code is written in this style, a smart compiler can do many things – it can parallelize the instructions, wait to evaluate results when needing them, and memorize the results since the results never change as long as the input doesn’t change.
sum(x, y) // sum is function taking x and y as arguments return x + y // sum is returning sum of x and y without changing them
There are no “for” or “while” loop in functional languages. Iteration in functional languages is implemented through recursion. Recursive functions repeatedly call themselves, until it reaches the base case.
example of the recursive function:
fib(n) if (n
In functional programs variables once defined do not change their value throughout the program. Functional programs do not have assignment statements. If we have to store some value, we define new variables instead. This eliminates any chances of side effects because any variable can be replaced with its actual value at any point of execution. State of any variable is constant at any instant.
Example:
x = x + 1 // this changes the value assigned to the variable x. // So the expression is not referentially transparent.
First-class functions are treated as first-class variable. The first class variables can be passed to functions as parameter, can be returned from functions or stored in data structures . Higher order functions are the functions that take other functions as arguments and they can also return functions.
Example:
show_output(f) // function show_output is declared taking argument f // which are another function f(); // calling passed function print_gfg() // declaring another function print("hello gfg"); show_output(print_gfg) // passing function in another function
In functional programming, we can’t modify a variable after it’s been initialized. We can create new variables – but we can’t modify existing variables, and this really helps to maintain state throughout the runtime of a program. Once we create a variable and set its value, we can have full confidence knowing that the value of that variable will never change.
Fact: Whatsapp needs only 50 engineers for its 900M users because Erlang is used to implement its concurrency needs.
Facebook uses Haskell in its anti-spam system.
Functional programming matters because it offers several benefits that align well with modern computing needs:
Functional programming is supported by several languages, each with unique features:
Object-Oriented Programming (OOP) and Functional Programming (FP) represent different approaches to software design:
Object-Oriented Programming (OOP)
Functional Programming (FP)
Encapsulates state within objects. State is mutable and can be changed by methods.
Encapsulates state within objects. State is mutable and can be changed by methods.
Encapsulates state within objects. State is mutable and can be changed by methods.
Encapsulates state within objects. State is mutable and can be changed by methods.
Achieved through classes and objects; methods define behavior, attributes define state.
Achieved through pure functions and function compositions; data is passed between functions.
Data and behavior are bundled together in objects; state changes occur through methods.
Data is immutable and managed through function applications.
Achieved through inheritance and polymorphism; classes can be extended and methods overridden.
Achieved through higher-order functions; functions can be passed as arguments and returned from other functions.
Functional programming provides a reliable paradigm in coding where the code is more reliable and encapsulated in reusable function with no side effects. These principles are helpful in generating more clean and maintainable code and are very beneficial in concurrent and parallel computing. Although there are disadvantages that may be observed, including the problem of recursion and immutability, the advantages usually overstep the shortcomings, which is why functional programming is an essential paradigm in the contemporary software engineering.
Functional programming is an approach to designing computation as the evaluation of mathematical function that can be done without modifying state and mutable data.
Pure function is a function that produces same results for same inputs and does not have any state changes of any variables outside the function.
Concurrency management is another area where functional programming excels, as the methods applied exclude such problems as side effects and shared mutable states.
Indeed, functional programming is employed in many practical applications, such as in the financial sector, and web development, and in distributed systems in which concurrency and code maintainability assets of functional programming are essential.