Chapter 1. Introducing functional programming

This chapter covers

  • Benefits and tenets of functional programming
  • Functional features of the C# language
  • Representation of functions in C#
  • Higher-order functions

Functional programming is a programming paradigm: a different way of thinking about programs than the mainstream, imperative paradigm you’re probably used to. For this reason, learning to think functionally is challenging but also very enriching. My ambition is that after reading this book, you’ll never look at code with the same eyes as before!

The learning process can be a bumpy ride. You’re likely to go from frustration at concepts that seem obscure or useless to exhilaration when something clicks in your mind, and you’re able to replace a mess of imperative code with just a couple of lines of elegant, functional code.

This chapter will address some questions you may have as you start on this journey: What exactly is functional programming? Why should I care? Can I code functionally in C#? Is it worth the effort?

We’ll start with a high-level overview of what functional programming (FP) is, and how well the C# language supports programming in a functional style. We’ll then discuss functions and how they’re represented in C#. Finally, we’ll dip our feet in the water with higher-order functions, which I’ll illustrate with a practical example.

1.1. What is this thing called functional programming?

1.2. How functional a language is C#?

1.3. Thinking in functions

1.4. Higher-order functions

1.5. Using HOFs to avoid duplication

1.6. Benefits of functional programming