The second piece of code has hidden inputs and outputs. It requires things, and causes things, but you could never guess what just by looking at the API

These hidden inputs and outputs have an official name: "side-effects"

Are Side Effects Bad?

we have to trust that the the hidden expectations of the original programmer were correct, and will remain correct as time marches on

Can we test this code? Not in isolation. Unlike a circuit board, we can't just plug into its inputs and check its outputs

What is a Pure Function

A function is called 'pure' if all its inputs are declared as inputs - none of them are hidden - and likewise all its outputs are declared as outputs

What is Functional Programming

Functional programming is about writing pure functions, about removing hidden inputs and outputs as far as we can, so that as much of our code as possible just describes a relationship between inputs and outputs

What is a Functional Programming Language

A functional programming language is one that supports and encourages programming without side-effects

Features of functional languages

First-Class Functions - Functional programming requires that functions are first-class, which means that they are treated like any other values and can be passed as arguments to other functions or be returned as a result of a function

Pure Function - 函数的结果只受函数参数影响, 且不影响外部变量 / 函数内部不使用能被外部函数影响的变量

Immutable Data - Purely functional programs typically operate on immutable data. Instead of altering existing values, altered copies are created and the original is preserved

Lazy Evaluation - Since pure computations are referentially transparent they can be performed at any time and still yield the same result. This makes it possible to defer the computation of values until they are needed, that is, to compute them lazily

Recursion

Recursion is heavily used in functional programming as it is the canonical and often the only way to iterate. Functional language implementations will often include tail call optimisation to ensure that heavy recursion does not consume excessive memory

Lazy evaluation avoids unnecessary computations and allows, for example, infinite data structures to be defined and used

In functional programming, programs are executed by evaluating expressions

In contrast with imperative programming where programs are composed of statements which change global state when executed. Functional programming typically avoids using mutable state

Benefits of functional programming

Functional programming is known to provide better support for structured programming than imperative programming. To make a program structured it is necessary to develop abstractions and split it into components which interface each other with those abstractions. Functional languages aid this by making it easy to create clean and simple abstractions. It is easy, for instance, to abstract out a recurring piece of code by creating a higher-order function, which will make the resulting code more declarative and comprehensible

Functional programs are often shorter and easier to understand than their imperative counterparts

Since various studies have shown that the average programmer's productivity in terms of lines of code is more or less the same for any programming language, this translates also to higher productivity