Skip to content
Justice Tech School

Swift Functions

Declare with func. Parameters have labels, and the return type follows ->. Use _ to drop an argument label. A single expression body returns implicitly.

Example
func add(_ a: Int, to b: Int) -> Int { a + b }
func greet(name: String = "friend") -> String { "Hello \(name)" }
print(add(2, to: 3))
print(greet())
print(greet(name: "Ada"))

Exercise

Return type arrow.

func f()  Int { 1 }

Quiz Test

Which symbol marks the return type?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.