What is functional programming?

Functional programming is a programming paradigm where the focus is on changing data through small expressions that don’t have side effects.

In other words, when multiples calls to any function occur with the same arguments, the result will always be the same. This is because the functions don’t depend on a local or global state, which produces different results depending on the state.

Functional programming is based on:

First class functions

Functions that can be treated like regular variables. Such functions can be passed as arguments to other functions, can be returned by other functions, and can be assigned as values to a variable.

svg viewer
var foo = function(){
return "Edpressos are fun!";
}
function Edpresso(greeting){
console.log("Educative's " + greeting());
}
Edpresso(foo);

Higher order functions

Functions that return other functions are called higher order functions.

svg viewer

Data immutability

Data cannot be modified after creation in functional programming. So for example, an object ‘n’ with the value of 5 will always have the value of 5.

Lazy evaluation

The evaluation of an expression in functional programming is delayed until the value is needed.

// Not lazy
var sum = 1 + 1;
// Evaluates 1 + 1 immediately
// Lazy
const lazySum = () => 1 + 1;
// Evaluates 1 + 1 upon invocation

Recursion

Most functional code is written with recursion instead of for-loops. In fact, some functional languages don’t even support for-loops!

Pure functions

Pure functions are functions whose return values are solely determined by the function’s inputs.

recursion
recursion
Copyright ©2024 Educative, Inc. All rights reserved