Your first Swift program

suggest change

Write your code in a file named hello.swift:

print("Hello, world!")

To launch a terminal, press Ctrl+Alt+T on Linux, or find Terminal in Launchpad on macOS. To change directory, enter cd directory_name (or cd .. to go back).

swift hello.swift

A compiler is a computer program (or a set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language), with the latter often having a binary form known as object code. (Wikipedia)

swiftc hello.swift

This compiles your code into a file named hello. To run it, enter ./ followed by that filename.

./hello

Code:

func greet(name: String, surname: String) {
    print("Greetings \(name) \(surname)")
}

let myName = "Homer"
let mySurname = "Simpson"

greet(name: myName, surname: mySurname)

The pieces are:

Running it using the REPL:

swift
func greet(name: String, surname: String) {
    print("Greetings \(name) \(surname)")
}
let myName = "Homer"
let mySurname = "Simpson"
greet(name: myName, surname: mySurname)

Press CTRL+D to quit from REPL.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents