Overview

2 Thinking in functions

In this chapter, the author builds a practical bridge between the mathematical idea of a function and the ways functions are expressed in C#. A mathematical function is described as a mapping from a domain to a codomain, where the output depends only on the input. In C#, types stand in for those sets, and a function’s type or signature acts like a contract that states what inputs it accepts and what outputs it produces. This perspective helps establish the functional mindset used throughout the book, while also acknowledging that programming functions can sometimes depend on more than just their explicit arguments.

The chapter then surveys the main C# constructs used to represent functions: methods, delegates, lambda expressions, and dictionaries. Static methods are presented as the most familiar and idiomatic choice, especially when they behave like mathematical functions. Delegates are introduced as type-safe function pointers that let functions be treated as values, with the built-in Func and Action families providing flexible, reusable types for many common signatures. Lambdas offer a concise way to define functions inline, and closures allow them to capture variables from the surrounding scope. Dictionaries are also reframed as function-like mappings, which is useful both for representing arbitrary associations and for techniques such as memoization.

From there, the chapter turns to higher-order functions, emphasizing their power to take functions as inputs, return functions as outputs, or do both. Examples such as Where and Sort show how HOFs can delegate decision-making to the caller, enabling separation of concerns and flexible reuse. The chapter also describes adapter functions, such as swapping argument order, and function factories, such as building predicates from parameters. A final example shows how HOFs can remove duplication by wrapping common setup and teardown logic around database operations, making the code shorter and clearer while preserving flexibility. The chapter closes by encouraging careful use of these abstractions so that functional power does not come at the expense of readability.

A mathematical function is simply a mapping. It maps elements from one set (the domain) to elements of another set (the codomain).
Where is a HOF that takes a predicate function as input. It uses the predicate to decide which elements to include in the returned list.
A HOF that iteratively applies the function given as an argument
A HOF that conditionally applies the function given as an argument
Here we define IsMod as a HOF that returns a function telling us whether a number is divisible by some given value. We then take the resulting predicate and use it as input to Where.
A HOF that wraps a given function between setup and teardown logic

Summary

  • Mathematical functions simply define a mapping between two sets.
  • You can represent functions in C# using methods, delegates, lambdas, and dictionaries.
  • FP leverages higher-order functions (HOFs), which are functions that take other functions as input or output); hence, the necessity for the language to have functions as first-class values.

FAQ

What is a function in the mathematical sense?A function is a mapping from a domain to a codomain: given an input from the domain, it produces one output in the codomain.
How does the book relate mathematical functions to C# types?In C#, types represent the domain and codomain of a function. For example, a function from char to char has the type char → char.
What is a function signature, and why is it important?A function signature is its interface: it describes the input and output types. It acts like a contract that says what kind of value the function accepts and what it returns.
What C# language constructs can represent functions?The main constructs are methods, delegates, lambda expressions, and dictionaries. Methods and lambdas are common, while delegates and dictionaries can also model functions in useful ways.
What is a delegate in C#?A delegate is a type-safe function pointer. It defines a signature and can hold a method or lambda that matches that signature.
What are Func and Action delegates used for?Func delegates represent functions that return a value, while Action delegates represent functions that return void. They provide standard delegate types for many common function shapes.
What is a lambda expression?A lambda expression is an inline way to declare a function. It is often used when the function is short and does not need to be reused elsewhere.
How can closures affect lambda expressions in C#?A lambda can capture variables from its enclosing scope. This means the lambda may depend on values outside its explicit parameters, which is called a closure.
How can a dictionary represent a function?A dictionary can model a function by storing mappings from keys in the domain to values in the codomain. This is especially useful for arbitrary mappings that are stored explicitly.
What is a higher-order function?A higher-order function is a function that takes another function as input, returns a function as output, or both. Examples include Where, Sort, and functions like SwapArgs or Connect.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free