As most of us are working on the object oriented languages day in and day out, it makes a little sense to know about other programming paradigms such as functional programming. In this article let’s understand about functional programming, its advantages and also see a few code samples in the F# language.
Functional programming is a programming model where you write the code similar to that of the mathematical functions. The functions act like the expression evaluators which produce consistent results for the given input. Functional programming is about what is required instead of instructing how the task should be executed in order to produce the result.
The following is a simple functional programming code written in F#.
let sum x y = (x + 10) * y let result = sum 25 5 printfn "%d" result
There are other popular functional programming languages such as Haskell, Erlang, Scala and so on.
Functional Programming Vs Procedural / Iterative Programming
As most developers are used to doing procedural coding it is important to understand the differences between the functional and procedural programming models.
1. Functional Programming (FP) is about what is required and Imperative Programming (IP) provides step by step instruction on how to proceed.
2. FP does not use any objects or states but IP works mostly on OOPS and is dependent on states.
3. FP works on immutable values whereas the IP values are mutable. In fact FP treats functions as values.
4. In FP the order of execution is not so important but for IP the hierarchical execution is inevitable.
What is Achieved Through Functional Programming?
In this section let us see the advantages of using functional programming and see in which form it helps us.
1. Code is concise and results in fewer lines of code.
2. Since the functions are not state dependent and mostly standalone ones it improves the unit testability of the code.
3. It paves the way for improved and easy implementation of parallelism.
4. Functional programming languages like F# do a high level of runtime type inference so you don’t have to declare the specific type during coding.
5. Functional programming leads to fast development as it doesn’t require the patterns to be followed like the statements ending with semi colon, functions enclosed with curly braces, creation of classes, concentrating on structuring the code, etc.
A few drawbacks are that it becomes difficult to organize the code and can get clumsy with lot of duplications as the code base grows. Also the code wouldn’t be as structured as the other models.
Different Types of Functions
A functional programming language will support writing different function types and let us look at those in this section.
A pure function is the one which has very few side effects. It is not dependent on any other data or function dependency. It does not cause any side effects to the program even if removed when not used anywhere.
The first code example provided in this article is a good example for a pure function.
First Class Function
The functions which can be passed as arguments to other functions, can be assigned to a variable, added to a data structure or can be set as the return value of another function is said to be a first class function. The first class functions are an integral part of functional programming model. The following sample is from F#.
Assigning the function to a variable:
let firstClassExpression = fun x y -> (x + 10) * y let result = firstClassExpression 100 20
Storing functions in a data structure:
//Named function1 let firstClassExpressionForSubtraction = fun x y -> x - y //Named function 2 let firstClassExpressionForSum = fun x y -> x + y //Adding the functions to a data structure let arithmaticFunctionList = [firstClassExpressionForSubtraction, firstClassExpressionForSum]
Higher Order Function
Higher order functions are the ones that accept functions as arguments and also may have the return value as functions itself. This is mostly used in building a composition of functions. Here is an example for a higher order function.
//Returns a function as a return value. arithmaticOperation is a higher order function let arithmaticOperation operator = let compute = fun x y -> if operator = "ADD" then x + y else x - y compute
There are also many more concepts in functional programming like recursion, pattern matching, etc. I will leave the rest for the readers to explore.
Since the model of functional programming is very different from imperative programming it may feel a little unfamiliar for the developers initially. But it becomes interesting when you get the rhythm going.
I hope this article provides an insight about the functional programming paradigm. Happy reading!