The problem variables solve
Imagine a program that tells a short story about a character called John who is 40 years old. Without variables, the name and the age are typed into every line that mentions them:
print("My name is John and I am 40 years old")
print("Coding at age 40 is really fun")
print("My son is named after me, John")
To make the character Andy, aged 32, you have to find and edit each occurrence. With three lines that is easy. With hundreds of lines it is slow, and one missed edit leaves a story that contradicts itself.
The same program with variables
A variable gives a value a name. Store the name and the age once, then place them into the text with string interpolation, where \(name) inside a string is replaced by the current value of name:
var name = "John"
var age = 40
print("My name is \(name) and I am \(age) years old")
print("Coding at age \(age) is really fun")
print("My son is named after me, \(name)")
My name is John and I am 40 years old
Coding at age 40 is really fun
My son is named after me, John
Now the story changes with two edits, the first two lines, and the print lines stay untouched:
var name = "Andy"
var age = 32
With these two lines in front of the three print calls above, the output begins My name is Andy and I am 32 years old. The facts live in one place, and everything else refers to them.
Variables can change while the program runs
A variable declared with var can also be reassigned partway through. Every line that runs afterwards sees the new value:
var name = "Andy"
var age = 32
name = "Mia"
age = 41
print("My name is \(name) and I am \(age) years old")
print("Next year I will be \(age + 1)")
My name is Mia and I am 41 years old
Next year I will be 42
The second line shows that a variable can feed a calculation, not just a printed sentence. Once a program needs the age in several places, one named value beats copies of a number scattered through the code.
The exercise
The starter code tells a story about someone's experience:
// Create variables here:
var experience = 5
var language = "Python"
// Print story here:
print("I have \(experience) years of experience with \(language)")
The task is to turn it into a story about 13 years of experience with Java, without editing the print line.
- Find the two declarations. Their names,
experienceandlanguage, stay as they are, and so does thevarkeyword. - Replace the number:
5becomes13. It stays a bare number with no quotes, because it is anInt. - Replace the text:
"Python"becomes"Java". It keeps its quotes, because it is aString. - Leave the
printline alone. Interpolation reads the new values.
The solution in Swift
// Create variables here:
var experience = 13
var language = "Java"
// Print story here:
print("I have \(experience) years of experience with \(language)")
The program prints I have 13 years of experience with Java.
Swift details
- Types come from the values. Swift infers that
experienceis anIntandlanguageis aString. A new value must be of the same type. Assigning"thirteen"toexperiencelater would be a compile error. - Interpolation takes any expression.
"\(experience + 1)"inserts the result of the calculation. varversuslet.varallows the value to change later. When a value never changes, Swift code normally useslet. The next lessons cover the difference.
Common mistakes
- Quoting the variable name. Outside
\( ), the wordexperiencein a string is plain text. - Changing the print line instead of the values. It works for this lesson's one line but defeats the purpose of having variables.
- Forgetting the backslash.
"(experience)"prints the parentheses and the word. Interpolation needs\(experience).
Key takeaways
- Store each fact once in a variable and refer to it by name.
- Interpolation,
\(name), puts a variable's current value into a string. - Changing a variable's value changes every line that uses it.
