Next: Checkoff
Up: Numbers and Constants
Previous: INTEGER and REAL

Constants

This topic is a complete change from what we've been discussing in the last two lessons, but it's important nonetheless. Let's look at another example program: ``radius.f'' (or view it directly).

This program computes the area of circles and the volume of spheres with a given radius. Notice that both computations involve the use of the mathematical constant PI, which is approximately 3.14.

What is wrong with writing the constant ``3.14'' directly into the program. (Hint: suppose the program were much bigger and used the value of PI dozens of times?)

Click here for the answer

It is much better to write programs so that changes can be made in one place. Given what we've already studied, what would be a way of accomplishing this?

Click here for the answer

Change the example program along these lines. When you get it working properly, consider this question: is there something strange about what we have just done?

Click here for the answer

Fortunately, there is a way to declare PI as a constant instead of as a variable. Let's look at a final example program to see how. See ``radius2.f'' (or view it directly).

Notice what we have done. We declare PI as a REAL, but then we immediately give it a value via a PARAMETER statement. Now any attempt to change the value of PI will be flagged by the compiler as an error. Try changing the value of PI via an assignment statement and see what happens.


Next: Checkoff
Up: Numbers and Constants
Previous: INTEGER and REAL

Hamlet Project
hamlet@cs.utah.edu