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 it's 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:

sway> var PI = 3.14159;
REAL_NUMBER: 3.1415900000

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.

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:

function h(w)
{
w / 2.0;
}

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:

sway> h(100);
REAL_NUMBER: 50.0000000

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:

sway> dh / dw;
REAL_NUMBER: 0.5000000000

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.

function ratio(w,dw)
{
var dh = h(w + dw) - h(w);
dh / dw;
}

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 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.[2] 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.

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.[3]

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.

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 . If we assume that ) is equivalent to ) then dh is or simply . 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 . 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, . 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 ?

9. The rate of damage reduction from armor is where . What is the relative growing of damage reduction with Armor? With EnemyLevel?