Start learning free

LESSON 9 OF 415 · VARIABLES

The Power of Variables in Swift: One Change, Whole Program

Lesson Read the full explanation

Now that we've learned about variables, let's see why they are so important in our programmers' life:

Consider this simple program which prints a story:

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")
print("I'd love him to be as cool as me at age 40")
print("It's so funny when my wife calls John we both respond")

Changing the story:

This is a perfectly working program. But what if I come back to it and want to change the character's name to Andy and his age to 32?

Well, I'm going to have to go through the entire program and manually change the name John to the name Andy and the age 40 to the age 32:

print("My name is Andy and I am 32 years old")
print("Coding at age 32 is really fun")
print("My son is named after me, Andy")
print("I'd love him to be as cool as me at age 32")
print("It's so funny when my wife calls Andy, we both respond")

This is not horrible as I had to go through only 5 lines of code. But what if my code consisted of hundreds of lines?

It's a disaster. Going through the whole code and manually changing the name and age is boring but more importantly it's super easy to make mistakes.


The story with variables:

This is where variables shine. If I used a variable to store the name of the character and another variable to store his age when I first wrote this code, it would have looked like this:

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)")
print("I'd love him to be as cool as me at age \(age)")
print("It's so funny when my wife calls \(name) we both respond")

Changing the story with variables:

Now whenever I want to change the character's name and age, I only have to change two lines of code, instead of possibly hundreds.

If I want my character to be named Andy and aged 32, I initialize the variables name with Andy and age with 32 and leave the rest of the code untouched:

var name = "Andy"
var age = 32

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)")
print("I'd love him to be as cool as me at age \(age)")
print("It's so funny when my wife calls \(name) we both respond")

Assignment
Follow the Coding Tutorial and let's witness the power of variables!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · VARIABLES

    The Power Of Variables in Swift, explained

    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.

    1. Find the two declarations. Their names, experience and language, stay as they are, and so does the var keyword.
    2. Replace the number: 5 becomes 13. It stays a bare number with no quotes, because it is an Int.
    3. Replace the text: "Python" becomes "Java". It keeps its quotes, because it is a String.
    4. Leave the print line 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 experience is an Int and language is a String. A new value must be of the same type. Assigning "thirteen" to experience later would be a compile error.
    • Interpolation takes any expression. "\(experience + 1)" inserts the result of the calculation.
    • var versus let. var allows the value to change later. When a value never changes, Swift code normally uses let. The next lessons cover the difference.

    Common mistakes

    • Quoting the variable name. Outside \( ), the word experience in 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.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free