Variable Definitions

When a number occurs many times in our program(s), we should give it a name using a <#60499#><#1545#>VARIABLE DEFINITION<#1545#><#60499#>, which associates a name with a value. One example is <#60500#><#1546#>3.14<#1546#><#60500#>, which we have used in place of #tex2html_wrap_inline72488#. Here is how we could give this number a name:
<#1551#>(define<#1551#> <#1552#>PI<#1552#> <#1553#>3.14)<#1553#>
Now, every time we refer to <#60501#><#1557#>PI<#1557#><#60501#>, DrScheme replaces it with <#60502#><#1558#>3.14<#1558#><#60502#>. Using a name for a constant makes it easier to replace it with a different value. Suppose our program contains the definition for <#60503#><#1559#>PI<#1559#><#60503#>, and we decide that we need a better approximation of #tex2html_wrap_inline72490# for the entire program. By changing the definition to
<#1564#>(define<#1564#> <#1565#>PI<#1565#> <#1566#>3.14159)<#1566#>
the improvement is used everywhere where we use <#60504#><#1570#>PI<#1570#><#60504#>. If we didn't have a name like <#60505#><#1571#>PI<#1571#><#60505#> for #tex2html_wrap_inline72492#, we would have to find and all instances of <#60506#><#1572#>3.14<#1572#><#60506#> in the program and replace them with <#60507#><#1573#>3.14159<#1573#><#60507#>. Let us formulate this observation as our second guideline: rawhtml9 Guideline on Variable Definitions rawhtml10 Give names to frequently used constants and use the names instead of the constants in programs. rawhtml11 Initially, we won't use many variable definitions for constants, because our programs are small. But, as we learn to write larger programs, we will make more use of variable definitions. As we will see, the ability to have a single point of control for changes is important for variable and function definitions. external ~<#70668#>Variable definitions are also useful to build spreadsheets in DrScheme. Consider the following example:
<#1579#>;; A Lemonade Stand<#1579#>
<#1580#>(define<#1580#> <#1581#>quantity<#1581#> <#1582#>100)<#1582#> 
<#1583#>(define<#1583#> <#1584#>price<#1584#> <#1585#>.15)<#1585#> 
<#1586#>(define<#1586#> <#1587#>var-cost<#1587#> <#1588#>.07)<#1588#> 
<#1589#>(define<#1589#> <#1590#>fixed-cost<#1590#> <#1591#>2.0)<#1591#> 
<#1592#>;; ---------------------<#1592#> 
<#1593#>(define<#1593#> <#1594#>gross<#1594#> <#1595#>(*<#1595#> <#1596#>quantity<#1596#> <#1597#>price))<#1597#> 
<#1598#>(define<#1598#> <#1599#>cost<#1599#> <#1600#>(+<#1600#> <#1601#>fixed-cost<#1601#> <#1602#>(*<#1602#> <#1603#>quantity<#1603#> <#1604#>var-cost)))<#1604#> 
<#1605#>(define<#1605#> <#1606#>net<#1606#> <#1607#>(-<#1607#> <#1608#>gross<#1608#> <#1609#>cost))<#1609#> 
Each variable in the program is like a cell in a worksheet program, but our ``cells'' have mnemonic variable names with a precise meaning. In the example, the variables above the line stand for the given numbers, and the variables below the line define the dependencies. By editing a given, we can examine various scenarios. For example, by changing <#60508#><#1613#>(define<#1613#>\ <#1614#>price<#1614#>\ <#1615#>.15)<#1615#><#60508#> to <#60509#><#1616#>(define<#1616#>\ <#1617#>price<#1617#>\ <#1618#>.20)<#1618#><#60509#>, we can examine the net profit for 20-cent glasses of lemonade instead of 15-cent glasses. Use a small number of such exercises if your students have difficulties understanding variables. If there is demand, we will put up some exercises that Ms.\ Karen Buras used in her spread-sheet class.
<#70668#>
<#1621#>Exercise 3.2.1<#1621#> Provide variable definitions for all constants that appear in the profit program of figure~#figprofit#1623> and replace the constants with their names.~ external Solution<#60510#><#60510#>