liveBook · Manning

Pure function

As explained in F# in Action: A Revised Edition of Get Programming with F#

Functional purity is the property of functions having deterministic outputs for given inputs and no observable side effects.

A pure function always returns the same output for the same inputs and has no observable side effects. Its result depends only on arguments, not on hidden global state, private mutable fields, file writes, or database changes. That makes the function’s behavior visible as a simple input-to-output contract. section 4.1.1 section 14.3.1

Use purity to model state changes as values rather than mutations. A driving function can accept the current gas and distance, then return updated gas; callers explicitly pass that returned value into the next step. Each intermediate state is inspectable, calls are repeatable, and ordering dependencies are clear. section 4.2.3

Pure functions are especially useful when you want code that is easy to reason about and test:

  • Test by arranging input values, calling the function, and asserting its returned result rather than inspecting changed object state or external systems. section 4.1.1 section 14.3.1
  • Reuse and cache computations whose results depend solely on supplied data. section 15.2.3
  • Separate data generation from external behavior: return text from one function, then let another function print it. section 15.2.3

Keep console output at the edge

For customer messaging, make a function such as describeCustomer return a greeting string. A separate describeToConsole function can pass that string to a console printer. The string-producing logic remains independently testable; the console-writing wrapper is the deliberately impure boundary. section 15.2.3 section 15.2.4

Listing 4.7 Managing state with immutable values
let drive gas distance =                      #1
    if distance = "far" then gas / 2.0
    elif distance = "medium" then gas – 10.0
    else gas – 1.0
 
let gas = 100.0                               #2
let firstState = drive gas "far"              #3
let secondState = drive firstState "medium"
let finalState = drive secondState "near"     #4

Compare mutable gas updates with an immutable drive function that receives gas and returns the next state for explicit chaining. section 4.2.3

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Defines purity and side effects, then works through immutable state, testing, caching, and separating output generation from I/O.
Examples
Uses gas consumption state transitions and customer greetings separated from console printing.
What it assumes
Introduces purity from inputs, outputs, and side effects without requiring prior F# or .NET knowledge.
What you can do after
Apply it by writing repeatable functions that return values and isolating database, console, and mutable-state work.
How it connects
Connects purity with side effects, immutability, unit testing, caching, higher-order functions, and dependency injection.

Also explained in

Compare how 19 books explain pure function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.