# The Science of Programming/A Little of This, A Little of That

In the last chapter, you were introduced to the idea of a variable. A variable is a spot in the computer's memory where you can store a value:
Here, I've named that spot x and I've stored the number 1000000 at that spot. Once, I've reserved that spot, I can change the value there by assigning a new value to the variable. Here's an example:
Notice that I did not use the word var this time. When I use var, it means reserve me a new spot. When I don't use var, it means update a previously reserved spot.
To learn more about assignment, please see Sway assignment.
What happens if we try to update a spot without reserving it first?
We get a message that y is undefined. This is the interpreter's way of telling us that we forgot to reserve a spot for y using var. Sway knows we didn't define the variable because it looked for it in the current environment and, failing, in the context environment and, failing, in the context of the context, and so, until it ran out of places to look.
The word variable implies that the value can vary, which is exactly what we did with x; we changed its value. In some programming languages, there are analogous beasties named constants, only once created, you cannot vary them. In Sway, there are no constants, per se. Should we need a constant, we will use a variable instead, but we will make a pledge not to change a variable's value. To remind us that we should not change its value, we will use capital letters (mostly) in naming the variable:
Making this pledge is known as following a convention. Note that variable names can be (and generally should be) more than a single letter. Note also that SPT in Chapter 3 of CME uses lower case letters early in the alphabet to indicate constants and lower case letters late in the alphabet to indicate variables. That is a different convention.

## Adding a little bit

Here's a problem that is a little bit simpler than the one from CME:
Consider a rectangle whose width and height are related. Suppose we increase the width a little bit. What happens to the height, assuming the new height is still one half of the new width? Since the height is calculated from the width, we say that width is the independent variable and that height is the dependent variable. First, let's define a function that gives us the height, h, given the width, w:
Note that when we write functions like this, it is almost always the case that the dependent variable is the name of the function and the independent variable is the name of the formal parameter. Use this rule to help you define the functions you need.
We can test out our new function:
This confirms our suspicion that if the width is 100 units, the height must be 50 units.
Calculus is often concerned with the change in the dependent variable given a change in the independent variable, particularly the ratio of the two changes.
Now, let's figure out that ratio for the above rectangle, using a small change in width:
The change in the height is simply the new height minus the old height. Now we can compute the ratio:
Another thing Calculus is concerned with is does this ratio change with different amounts of change or different values for the independent variable? The lazy person's way to answer these questions is, you guessed it, to write a function to do the work.
Note that we pass in the values of w and dw and then compute the ratio. This allows us to try a wide variety of values quite easily:
This ratio function is quite useful! We see that the ratio of ${\frac {dy}{dx}}$ does not seem to ever change. But suppose we now start looking at rectangles with a different relationship between width and height. All of a sudden, our ratio function is looking a lot less useful. This is because we hard-wired the height function. We can fix this by passing in the height function as a third argument. Now we can easily try out new height functions. By doing so, we have made the ratio computing function more general.
It should work just the same:
Let's try a different height function:
With this height function, the height g, grows as the square of the width. What does ratio produce now? Let's try one width and ever smaller changes:
It's pretty clear that if the change in width becomes infinitely small, the ratio when the width is 100 will be 200.
Now let's try a different width and ever smaller changes:
For this new width, 200, the ratio appears to converge on 400. Also, for the last test, we got a value that was a little unexpected. This should be a warning to you when dealing with real numbers.
For the height function g, we see that different starting widths yield different ratios. A good deal of calculus is for explaining why such ratios change. Also, this process of computing ratios is known as the taking the derivative.

## Questions

All formulas are written using Grammar School Precedence.
1. Does it matter what variable name we use as a formal parameter when defining a function?
2. Draw a picture of a labelled rectangle so that the height is the independent variable, keeping the relationship that the height is one half the width.
3. In the function ratio, dh is computed from $h(w+dw)-h(w)$. If we assume that $(w+dh$) is equivalent to $(w)+h(dw$) then dh is $(w)+h(dw)-h(w)$ or simply $(dw)$. Define a new function named ratio2 to test whether this assumption is valid.
4. Which function is better for very small changes? Why?
5. Consider a rectangle whose area never varies. That is, if the width is increased, the height decreases to compensate so that the area of the rectangle remains the same. Define a function that computes the height of this rectangle given the width and the area.
6. Do we observe the same behavior in the ratios as we did with function g?
7. "Change is constant," but does not have to be a constant. Most of us are comfortable with the notion of a constant rate of change. For instance the change in height versus distance as described by a straight line, for instance $y=5x+4$. Use the ratio function to explore how y changes as x changes. Produce a table of y, x, and dy/dx.
8. The previous problem had a constant rate of change. Often, however, the rate of change of the dependent variable changes with the independent variable. Let's take as an example the function for area of a square, $y=x^{2}$. First of all, what is the change in the area as x changes from 2 to 4? Draw a sketch of this situation, labeling dy and dx. Do the same thing as x changes from 2 to 3, 2 to 2.5, 2 to 2.1, and 2 to 2.01. What do you observe? Try the same thing, but now start with x = 4. The point is that the rate of change in the area, itself changes depending on the length. The function of x that expresses this relationship between the change in y and x is called the derivative, dy/dx. Based on your experiments, what is the derivative of $y=x^{2}$?
9. The rate of damage reduction from armor is ${\mathit {Reduction}}={\mathit {Armor}}/({\mathit {Armor}}+X)$ where $X=467*{\mathit {EnemyLevel}}-22167$. What is the relative growing of damage reduction with Armor? With EnemyLevel?

## Footnotes

* ↑ This figure is drawn using Cheesy ASCII (tm) Graphics (for fast loading).
* ↑ We hard-wire something when we treat it as a constant, something that doesn't change.
* ↑ You can trust integers, because they are exact. But you should never trust real numbers completely, because they are approximations. Sway real numbers are only precise to 15 significant digits.
← How Low Can You Go? · The Simplest Things →