Building Programs with Functions

In functional programming, functions are the primary tools for building a program. You can’t create a useful program without writing or using functions. They receive data, complete some operation, and return a value. They are usually short and expressive.

We combine multiple little functions to create a larger program. The complexity of building a larger application is reduced when the functions have these properties:

  • The values are immutable.
  • The function’s result is affected only by the function’s arguments.
  • The function doesn’t generate effects beyond the value it returns.

Functions that have these properties are called pure functions. A simple example is a function that adds 2 to a given number:

 add2 = ​

Get Learn Functional Programming with Elixir now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.