Pure function

def double(i: Int): Int = i * 2
def sum(list: List[Int]): Int = list match {
    case Nil => 0
    case head :: tail => head + sum(tail)
}
  • The function’s output depends only on its input variables.
  • It doesn’t mutate any hidden state.
  • It doesn’t have any “back doors”: It doesn’t read data from the outside world (including the console, web services, databases, files, etc.), or write data to the outside world.
  • As a result of this definition, any time you call a pure function with the same input value(s), you’ll always get the same result.
Pure function → belongs to → Functions.

Pure function — Structure map

Clickable & Draggable!

Pure function — Related pages: