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 a function defined inside another scope. In F#, it can serve as a small helper without making that helper available elsewhere. Keeping bindings local also makes it easier to see where they are used. 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}."

The greeting function defines makeFullName, calls it to produce fullName, then uses the result. makeFullName is available only inside greetingTextWithFunction. section 3.1.3

An inner function can also use a value declared before it in an outer scope. Using an outer value such as city this way is called capturing it; the compiler handles the capture. The reverse does not work: a binding declared inside a scope is unavailable after that scope. 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, shows a local call, and explains outer-value capture without detailing compiler mechanics.
Examples
Uses a greeting-formatting helper and a city binding read from an outer scope.
What it assumes
Assumes familiarity with basic function and let-binding syntax while defining nesting and explaining capture.
What you can do after
Apply it via F# to define a local helper and use a preceding outer binding inside it.
How it connects
Connects inner functions to lexical scope, function values, and captured values.

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.