Encapsulation

When we design a program to control a traffic light, we probably don't want to control just one traffic light, but several. Similarly, when we design a program to manage names and phone numbers, we might wish to manage several address books, not just one. Of course, we could copy the code for a traffic light controller (or an address book manager) and rename the state variables, but copying code is bad. Furthermore, we might wish to create so many traffic lights that copying code is plain impractical. The proper solution is to use the power of abstraction. Here we abstract over several instances of the address book program and the traffic light program and so on. This differs from the notion of abstraction in part~#partabstract#50580> because it involves state variables, but the idea and even the technique is the same. We encapsulate the state variables and the functions in a <#69047#><#50581#>local<#50581#>-expression<#69047#> and thus give ourselves the power to create as many different versions as necessary. We learn how to encapsulate state variables in the first subsection, and practice it in the second one.