Functional Programming – Part 1: Pure Functions, Testability, and Referential Transparency
October 6, 2019    |    Basics    |    Programming    |    Scala

What is Functional Programming?

Functional Programming is a style of programming in which most functions are “pure.” A “pure” function is a function, given an input, always returns the same output without creating any side-effects. An example of a side effect would be a change to a variable outside of the current function scope.

Function programming increases the testability of your code by narrowing the scope of each function.

Here is an example of an impure function in Scala:

var count = 0
def buildGreetingString(name: String): String = {
  count += 1
  val greeting: String = "Hello, "++name
  return greeting
}
buildGreetingString("Richard")
buildGreetingString("Richard")
buildGreetingString("Richard")

// Output
scala> count
Int = 3

The above function is not pure because it is changing a variable outside of its own scope. This is an overly simple example, but it illustrates a likely scenario imperative programming causes.

In order to track the count of our greeting function, we need to move the count outside of the function. Here is the greeting function in a pure form:

def incrementCount(current: Int): Int = {
}
def buildGreetingString(name: String): String = {
  val greeting: String = "Hello, "++name
  return greeting
}
buildGreetingString("Richard")
incrementCount()

The reason this function is pure is that it doesn’t have any unwanted side effects. Incrementing the counter is now a separate function.

How does Function Programming differ from imperative programming?

Imperative programming and Functional programming are two paradigms. In imperative programming, the programming tells the program: “Do this, do that, do this, etc…”. The programmer uses statements to change the state of the application. [1]

Functional programming requires a different way to think about what you are programming. In functional programming, the programmer writes code that does NOT directly change the state of the application. Instead of statements or commands, a functional program uses expressions and declarations to control the program.

How can you tell if a function is “pure”?

Referential Transparency is the concept that a function’s body is can be replaced by the resulting value of the function.[2]

Here is a terribly simple example of a pure function:

def getNumber(num: Double): Double = num

The expression block of getNumber is the exact same as the evaluated function. We know that a function is pure if calling the function would give the same value as if we just wrote the value itself.

getNumber(6) == 6

References

  1. Imperative Programming. https://en.wikipedia.org/wiki/Imperative_programming. Retrieved 2019-10-05
  2. What Is Referential Transparency? Pierre-Yves Saumont. https://www.sitepoint.com/what-is-referential-transparency/. Retrieved 2019-10-6

Leave a Reply

Your email address will not be published. Required fields are marked *