Functional Programming is All About Expressions

Get familiar with the way functional programming treats everything as expressions.

Functional programming is all about evaluating expressions to values. For someone new to functional programming, this concept can be hard to grasp at first. A great way to understand it is by contrasting it with imperative programming.

In his Turing Award lecture, “Can Programming Be Liberated from the von Neumann Style? A Functional Style and Its Algebra of Programs,” John Backus shows that an imperative programming language—such as C and Java—splits language elements into two worlds, expressions and statements. Expressions are those constructs that evaluate to values. In C or Java, we can define arithmetic expressions like 1 + 2, boolean expressions like true || false, and string expressions like "Hello". They all evaluate to values. Statements, on the other hand, are commands that perform something like variable assignment s = s + 1, if statements for branching, and for/while loops for executing statements repeatedly. Statements are characterized by their side effects.

Functional programming languages do not have any statements, no variable assignments, no if statements, and no for/while loops. Therefore, instead of variable assignments, we pass values around through function arguments and return values. Conditionals like if are expressions rather than statements. To formulate repeated computations, functional programming relies on recursive functions instead of loops. Everything is an expression in the functional paradigm.

The following diagram compares the worldview difference between imperative and functional programming languages.

Get hands-on with 1400+ tech skills courses.