In both mathematics and life in general, it is convenient to have names for things. It would certainly be awkward to communicate if we did not have words and proper names for commonly used things. In mathematics, you solve word problems by choosing variable names to refer to things in the problem. For example, you might say that name s stands for "the speed of train A".
In this chapter we look at how you can give something a name in Cinnameg.
In mathematics you usually choose single-letter names, such as x and y, for variables. But in a programming language, we need to allow longer names. You can use any sequence of one or more letters for a name. For example, you can call something giraffe.
You can also include digits (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9) in names, provide the name begins with a letter. For example, x1 and horse52 can be used as names.
The case of a letter indicates whether the letter is upper case, such as R, or lower case, such as r. Both Cinnameg and Java pay attention to case. That means that R and r are considered different letters. If you decide to call something giraffe, then keep calling it exactly giraffe, not Giraffe or GIRAFFE.
In Cinnameg, only use names that start with a lower case letter.
Names are not allowed to contain spaces. If you want to use a multi-word name, a common convention is to capitalize the first letter of all but the first word. For example, you might call something theTallGiraffe, or myFavoriteNumber. The choice of what to call something is yours. Just be sure to decide exactly how to spell the name, including which letters are capitalized, and stick with that decision.
Remember that we want to strive for simplicity and elegance. You can go a long way toward that goal by choosing names for things that are sensible and informative. Suppose that you have a name that refers to the length of a word. (Just how you get the length of a word is something that we will see later.) Here are some good and bad choices for that name.
How about calling it x? That is short and simple, but it tells you nothing at all about what x is. In order to figure out what x is, you find yourself forced to examine the program in great detail, in effect reverse-engineering the program. That is unpleasant.
You know that the length of a word is a number. Should you call it number? Probably not. The problem is that there are probably a lot of numbers in your program. Calling something number is not very informative.
Should you call the length of the word word? Definitely not. The length of a word is not a word!
How about lengthOfWord? That sounds like a good choice. It is clear and informative.
Sometimes you prefer somewhat shorter names. An abbreviation such as wordLen is also reasonable.
If you want to name the speed of train A, choose a name such as speedOfTrainA.
We will encounter quite a few cases where we are not really sure what something is, and short names such as x and y are appropriate. In those cases, short and simple names are the best choice.
In Cinnameg, you give a name name to the value of expression e by writing
Let name = e.For example, writing
Let wolf = 9 + 1.makes wolf name the number 10. Notice that period at the end. It is very important to write the period. Think of a Let phrase as a sentence, and the period is the end of the sentence. The compiler will be very unforgiving if you forget the period.
Once you have defined what a name refers to, you can use that name in expressions. For example,
Let x = 50. Let y = 2*x + 1.makes x a name for 50 and y a name for 101.
Both Cinnameg and Java allow you to break a line whereever you want to. For example, a single let phrase can extend across more than one line if you need it to. For example,
Let numberOfApples = (100000 * 43 + 22) `div` (80 + 1).illustrates a two-line let. The end of the line is treated like a space. Notice how the second line is indented. There is a lot more to say about indentation, but for now just use the example.
In programming language terminology, a phrase such as
Let r = 20.is called a statement. In particular, we call it a Let-statement. In English, a statement states a fact. But in programming languages, the term statement has come to mean the same thing as a command. A statement tells the program to do something. For example, statement
Let x = r + 1.says to computer the value of r + 1, and then to make x name the result.
[solve] Is theDog an allowed name?
[solve] Is one2three an allowed name?
[solve] Is my dog an allowed name?
[solve] How would you give compute the value of expression 123456789 `mod` 23 and call the result rem?
[solve] Is statement
Let 8 = r.allowed?
[solve] In programming language terminology, what does the term statement mean?
[solve] Can a statement be more than one line long?
A name can contains letters and digits, provided it starts with a letter. A name cannot have any spaces in it.
The case of letters matters. Names Q and q are not the same, and cannot be used interchangeably.
In Cinnameg, it is important to choose names that start with lower case letters.
A statement is a command. It tells the program to do something.
A Let-statement computes a value and gives it a name. For example,
Let myAge = 22.says that myAge names value 22. When you use myAge, you are using 22. The name is on the left-hand side and the value (or expression that computes the value) is on the right-hand side of =. There must be a period at the end.
Use parentheses to group things in an expression, but not square brackets or curly braces.
If you encounter mistakes that you cannot understand, ask questions.