concept pure function in category functional programming

This is an excerpt from Manning's book Grokking Functional Programming.
Shopping cart discounts
Our task is to write a shopping cart that is able to calculate discounts based on the current cart contents. Namely:
We split the requirements into two separate sets that are fulfilled by different pieces of code! This way we have smaller, independent functions/classes, which means they are easier to read and write. In software engineering, this is called separation of concerns: every piece of code has its own responsibility and “is concerned” only about it. To see it in action, let’s go through all our requirements and see how they are fulfilled:
![]()
Passing state as an argument
The getTipPercentage function calculates the discount instead of using a stored value, but it still uses external names value to do the calculation. We need to pass the state as an argument and make the function static. The getTipPercentage function is now pure! Others, however, are not...
class TipCalculator { private List<String> names = new ArrayList<>(); public void addPerson(String name) { #A names.add(name); #B } public List<String> getNames() { #C return names; } public static int getTipPercentage(List<String> names) { if(names.size() > 5) { return 20; } else if(names.size() > 0) { return 10; } else return 0; #D } }

This is an excerpt from Manning's book Functional Programming with Kotlin.
The answer is that functional programming is a restriction on how we write programs, but not on what programs we can express. Over the course of this book, we’ll learn how to express the core of our programs without side effects, and that includes programs that perform IO , handle errors, and modify data. We’ll learn how following the discipline of FP is tremendously beneficial because of the increase in modularity that we gain from programming with pure functions. Because of their modularity, pure functions are easier to test, reuse, parallelize, generalize, and reason about. Furthermore, pure functions are much less prone to bugs. In this chapter, we look at a simple program with side effects and demonstrate some of the benefits of FP by removing them. We also discuss the benefits of FP more generally and work up to defining two important concepts — referential transparency and the substitution model.
Figure 1.2. A pure function, does only what it states without side effect.
![]()
In this chapter, we’ll learn the concept of functional data structures by writing our own implementations of a singly linked list and tree. We will also learn about the related processing technique of matching, and get lots of practice writing and generalizing pure functions.
This chapter has a lot of exercises, particularly to help with this last point—writing and generalizing pure functions. Some of these exercises may be challenging. Always try your best to solve them by yourself, although helpful tips and pointers may be found at the back of the book in Appendix A. On the occasions when you really get stuck, or want to confirm that your answers are correct, you may consult Appendix B for complete solutions. Try to use this resource only when you absolutely must! All the source code for the samples and exercises are also available in our GitHub repository (https://github.com/fpinkotlin/fpinkotlin).

This is an excerpt from Manning's book Functional Programming in C++.
One of FP’s most powerful ideas is pure functions: functions that only use (but don’t modify) the arguments passed to them in order to calculate the result. If a pure function is called multiple times with the same arguments, it must return the same result every time and leave no trace that it was ever invoked (no side effects). This all implies that pure functions are unable to alter the state of the program.
This is great, because you don’t have to think about the program state. But, unfortunately, it also implies that pure functions can’t read from the standard input, write to the standard output, create or delete files, insert rows into a database, and so on. If we wanted to be overly dedicated to immutability, we’d even have to forbid pure functions from changing the processor registers, memory, or anything else on the hardware level.
This makes this definition of pure functions unusable. The CPU executes instructions one by one, and it needs to track which instruction should be executed next. You can’t execute anything on the computer without mutating at least the internal state of the CPU. In addition, you couldn’t write useful programs if you couldn’t communicate with the user or another software system.
Because of this, we’re going to relax the requirements and refine our definition: a pure function is any function that doesn’t have observable (at a higher level) side effects. The function caller shouldn’t be able to see any trace that the function was executed, other than getting the result of the call. We won’t limit ourselves to using and writing only pure functions, but we’ll try to limit the number of nonpure ones we use.

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.
Elm (http://elm-lang.org/) is a strongly typed, purely functional client-side language that compiles to JavaScript. The syntax is terse, similar to Haskell or F#, but the language and tooling are much more user-friendly. It includes a framework that takes care of managing state and performing side effects. As a result, the programmer only writes pure functions. Simply put, Elm puts any existing JavaScript framework to shame. If you’re a full-stack web developer, consider using Elm for the front end.
In summary, pure functions have no side effects, and their output is solely determined by their inputs.
The deterministic nature of pure functions (that is, the fact that they always return the same output for the same input) has some interesting consequences. Pure functions are easy to test and to reason about.[2] Furthermore, the fact that outputs only depend on inputs means that the order of evaluation isn’t important. Whether you evaluate the result of a function now or later, the result will not change. This means that the parts of your program that consist entirely of pure functions can be optimized in a number of ways:
2More theoretically inclined authors show how you can reason about pure functions algebraically to prove the correctness of your program; see, for example, Programming in Haskell, second edition, by Graham Hutton (Cambridge University Press, 2016).
Listing 2.4. List formatter refactored to use pure functions only
using static System.Linq.Enumerable; static class ListFormatter { public static List<string> Format(List<string> list) { var left = list.Select(StringExt.ToSentenceCase); var right = Range(1, list.Count); var zipped = Zip(left, right, (s, i) => $"{i}. {s}"); return zipped.ToList(); } }

This is an excerpt from Manning's book Functional Programming in JavaScript.
In this chapter, I define what functional programming is and tell you how and why it’s useful and important. I introduce the core principles of immutability and pure functions and talk about FP techniques and how those techniques affect your approach to designing programs. These techniques allow you to easily pick up reactive programming and use it to solve complex JavaScript tasks. But before we can get into all this, you need to learn why thinking functionally is important and how it can help you tackle the complexities of JavaScript programs.
If you’re unable to create and modify objects or print to the console, what practical value would you get from a program like this? Indeed, pure functions can be hard to use in a world full of dynamic behavior and mutation. But practical functional programming doesn’t restrict all changes of state; it just provides a framework to help you manage and reduce them, while allowing you to separate the pure from the impure. Impure code produces externally visible side effects like those listed earlier, and in this book I examine ways to deal with this.

This is an excerpt from Manning's book Functional Programming in Scala.
We said earlier that FP means programming with pure functions, and a pure function is one that lacks side effects. In our discussion of the coffee shop example, we worked off an informal notion of side effects and purity. Here we’ll formalize this notion, to pinpoint more precisely what it means to program functionally. This will also give us additional insight into one of the benefits of functional programming: pure functions are easier to reason about.
In the first chapter of this book, we introduced the concept of referential transparency, setting the premise for purely functional programming. We declared that pure functions can’t mutate data in place or interact with the external world. In chapter 13, we learned that this isn’t exactly true. We can write purely functional and compositional programs that describe interactions with the outside world. These programs are unaware that they can be interpreted with an evaluator that has an effect on the world.