liveBook · Manning

Inner function

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

A nested function is a function defined inside another scope or function.

An inner function is defined inside another scope or function. In F#, functions are values, so a helper can be created exactly where it is needed rather than exposed more widely. section 3.1.3

This keeps helpers local: `makeFullName` exists only while `greetingTextWithFunction` is being defined. Use that narrow scope when a function supports one operation, while remembering that a one-off helper may add unnecessary ceremony. section 3.1.3

let greetingTextWithFunction person =
    let makeFullName fname sname =                  #1
        $"{fname} {sname}"                          #2
    let fullName = makeFullName "Frank" "Schmidt"   #3
    $"Greetings {fullName} from {person}."

A greeting function defines `makeFullName`, calls it locally, and returns the completed greeting. section 3.1.3

An inner function can read values declared before its nested scope. Referencing such an outer value is called capturing; the compiler handles it, though it can matter in high-performance scenarios. section 3.1.3

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

How deep it goes
Defines nested functions and capturing, while only noting performance implications without further detail.
Examples
Demonstrates a local `makeFullName` helper that builds a greeting from first and last names.
What it assumes
Assumes basic F# `let` bindings while introducing nested scopes and outer-value capture.
What you can do after
Apply inner functions to place helper behavior beside the operation that uses it.
How it connects
Connects inner functions with scope, captured values, immutability, and compiler errors.

Also explained in

Compare how 5 books explain inner 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.