Skip to content
Theory · First program · lesson 2 of 4

Output with print and comments

In the previous lesson, the program printed one line. Now let's learn to print several values at once and to decide how print separates them and how it ends the line.

A cook goes through a recipe from top to bottom. A program in the file main.swift runs the same way—line by line, in the order the lines are written:

SWIFT
print("Hello!")       // Hello!
print("a", 1)         // a 1
print("Year:", 2014)  // Year: 2014

Let's see what happens when it runs:

  1. The first line prints Hello!.
  2. The second line prints a 1 below it, on a new line of output.
  3. The third line prints Year: 2014.
  4. There are no more lines, and the program ends.

Each call to print prints its own line of output, so the output comes in the same order as the lines of the program.

This is where the recipe and the program part ways. A recipe gives one dish, and only at the very end, while a program outputs text as it works: each call to print adds its own part to the output.

Several values

The values in the parentheses of a function call are called arguments. print("Year:", 2014) has two arguments:

  • "Year:" in quotes is text;
  • 2014 without quotes is a number.

Arguments are separated by commas. print prints them in order and puts a space between them: the text "Year:" has no space, the function added it. Text is printed without quotes, and a number as digits.

The separator and terminator labels

print has two special arguments that change how it prints. Each of them is preceded by a name with a colon: separator: or terminator:. Such a name in front of a value is called an argument label. From the label, Swift knows that this is not one more text to print but a setting.

Labeled arguments are written after the ordinary ones, and separator always comes before terminator. You can leave out either of them: then print uses the default value.

SWIFT
print("a", "b", "c", separator: ", ")  // a, b, c
print("Loading", terminator: "...")
print("done")                          // Loading...done

The separator argument

separator sets what print puts between neighboring values. By default, it is a space. In the first line of the example, separator: ", " replaced the space with a comma and a space, and the result is a, b, c.

No separator goes after the last value. Empty quotes "" glue the values together: print("a", "b", separator: "") prints ab.

The terminator argument

terminator sets what print prints at the very end, after all the values. By default, it is a line break, so the next print starts on a new line. In the print box below, the line break is shown as "\n": the backslash and the letter n together stand for this one invisible character.

Let's take apart the second and third lines of the example:

  1. print("Loading", terminator: "...") prints Loading, then ..., and no line break.
  2. print("done") continues the same line, and together they give Loading...done.

print() with no arguments prints only the terminator, that is, an empty line.

Step through the program below and watch the print box: it shows which separator and terminator each call received, even when the call has no labels.

LiveHow print builds a linestep 1 of 7 · frames: 2

There are no labels, so print uses the defaults: a space between values and a line break at the end.

Watch the print box: every call has a separator and a terminator, even without labels

Remember the main point of this program: until a terminator breaks the line, the next print adds its values to the same line.

Comments

A comment is text in a program that the compiler skips. It is written for the people who read the code. Swift has two kinds of comments.

Single-line comment

A single-line comment starts with // and runs to the end of the line. In the course's examples, such comments show what a line prints: they do not appear in the output.

Multi-line comment

A multi-line comment starts with /* and ends with */, with any number of lines between them. This is handy for turning off part of a program for a while:

SWIFT
print("one")    // one
/*
print("two")
*/
print("three")  // three

The line print("two") stands inside the comment, so the compiler skips it, and two is not printed.

One command per line

Each command stands on its own line. Swift does not require a semicolon at the end of a line, as some other languages do, and Swift code usually leaves it out.

Common mistake

The labels are written in the other order:

SWIFT
print("a", "b", terminator: "!", separator: "-") // ✗
TEXT
error: argument 'separator' must precede argument 'terminator'

The program does not compile: the message says that separator must come before terminator. The order of print's labels is fixed, so swap them: print("a", "b", separator: "-", terminator: "!").

Key points

  • The lines of main.swift run from top to bottom, and by default each call to print prints its own line of output.
  • print prints several arguments separated by spaces: text without quotes, numbers as digits.
  • separator: sets what stands between the values, and terminator: sets what is printed at the end instead of a line break.
  • Labels are written after the values, and separator: comes before terminator:.