Functional Programming is a Declarative style of Programming Paradigm for writing computer programs.
But, What are Functions ?
Functions in general, applies computation on given input and returns the output. It relates input to an output.
f(x) = x + 2;
f(1) = 1 + 2 = 3;
f(2) = 2 + 2 = 4;
Above mentioned is a simple function that adds 2 to the input value and returns output. It relates value [1,2] => [3,4]. Similarly, a function in computer programming is a block of instruction that performs computation on given input and returns the output.
Functional Programming is such a style of writing computer programs using these tiny functions that works together and perform required computation.
Functions in Functional Programming
The philosophy of functional programming is to maintain certain characteristics while writing functions in a computer program. These characteristics are the fundamental nature of functional programming that describe what shall be the behaviour of a function. These are as follows :
Declarative
A function must be declarative, it simply tells what to compute, without specifying how to compute it.
f(x) = x * 4; 👍
Declarative function that tells to multiply input by 4;
f(x) = { y = x + x; z = x + x; return y + z;} 👎
Non-Declarative function that specify how to multiply input by 4;
Pure
A function must give the same output for a given input value, at any period of time. It is not dependent upon anything outside the function definition.
f(x) = It’s never too late; 👍
Pure function that will always return It’s never too late
f(x) = If today’s Friday or Saturday then It’s never too late else It’s late. 👎
Impure function that consider day for returning value. The value is not predictable. It can change. So a function that performs anything which is unpredictable is not a pure function. The condition or the execution is dynamic or unfixed in an impure function.
#development #functional-programming #software #software-programming #declarative-programming #function