Chapter 4. Imperative Programming
Until now, most of the programs we’ve written have been pure, meaning that they never changed state. Whenever a function does something other than just return a value, it is known as a side effect. Although pure functions have some interesting features (e.g., composability), the fact of the matter is that programs aren’t interesting unless they do something: save data to disk, print values to the screen, issue network traffic, and so on. These side effects are where things actually get done.
This chapter covers how to change program state and alter control flow, which is known as imperative programming. This style of programming is considered to be more error prone than functional programming because it opens up the opportunity for getting things wrong. The more detailed the instructions given to the computer to branch, or write certain values into memory, the more likely the programmer will make a mistake. When you programmed in the functional style, all of your data was immutable, so you couldn’t assign a wrong value by accident. However, if used judiciously, imperative programming can be a great boon for F# development.
Some potential benefits for imperative programming are:
Improved performance
Ease of maintenance through code clarity
Interoperability with existing code
Imperative programming is a style in which the program performs tasks by altering data in memory. This typically leads to patterns where programs are written as a series of statements ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access