Introduction to Functional Programming — FP

Md Jubayar Hosan
Nov 6 · 3 min read

Functional Programming-FP is a programming paradigm where we try to write code like as pure mathematical function style. Recently it’s being popular in mainstream. Usually FP is widely used in academia. One of the main reasons being popular is software needed to design better in order to leverage multi core cpu. FP is better to write code in thread safe, clean and optimal code over OOP in multi core cpu. Take full advantage of multi core cpu is a big reason for the rise and popularity of FP.

History of functional programming : Lambda-Calculus, Alonzo Church, who was a mathematician and logician, and is considered one of the founders of Computer Science invented Lambda-Calculus, is mathematical language in the 1930’s. In some sense, Lambda-Calculus is the first programming language. Lambda-Calculus is a very abstract mathematical language. Some principles of Lambda-Calculus can be found in modern functional languages.

In Object Oriented Programming, you focus on object, And in Functional Programming, you will focus on function. Some modern programming language support both OOP and FP feature. Pure functional programming meet some criteria for being FP. To write code in functional programming style in some modern language such as — Kotlin, Swift, you have to follow these criteria. Functional programming criteria is described in below :

  1. Pure function — No side effect, Referential transparency
  2. First class citizen
  3. Higher order function

Functional programming inspired from mathematical function. In mathematical sense function will take input and return output. For a input, it’s output will be same always.

Write function in math: f(x) = x + 1
If x = 5, f(5) = 6
For all situation f(5) will be 6.
Function in kotlin : fun increment(x: Int) = x + 1
If x = 5, increment(5) will always return 6

Pure function is a function that will return the same value for a fixed input value in all situations. If you see the increment function, it’s return value will be the same for any condition.

private var times = 12 
fun loanAmount(salary: Int) = salary * times

This loanAmount function is not a pure function. Because it’s result depends on a value times which can be changed any time.

Pure Function has no side effects. That means it will not change anything outside of function.

In mathematical sense, function is stateless — no memory from previous history and for the future. That means global variable will be immutable. No variable, no memory, no loops.

Referential transparency is a characteristic for pure function. Pure function is just like a data table. Expression or function can be replaced by it’s value without changing the program behavior. Make sure your output type are referential transparency.

In Functional programming, function is considered a First Class Citizen. That means, you can assign a function in a variable like as you can assign an object in a variable. You can send function as an argument value in function or return function as a return value from other higher order function. For example :

private var listener = this::handle 
private fun handle(text: String) = TODO()

Here you assign your handle function in listener variable.

Functional programming support Higher Order Function to write code. Higher order function is a function that takes functions as a parameters or returns a function. In Functional programming all functions must have a function type like as object type in OOP.

fun higherOrderFun(str: String, myFunc: (String) -> Unit) {
myFunc(str)
}

In this function we send a function as a argument parameter, So it is a example of higher order function.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade