Functions
Declare a function with
func name(parameter: Type) -> ReturnType { ... }.
Example: a function call that reads naturally
func greet(name: String) -> String {
return "Hello, \(name)!"
}
print(greet(name: "Chidi"))
Hello, Chidi!
Notice the call site: greet(name: "Chidi"), not just
greet("Chidi"). Swift's function calls typically include
argument labels, which makes call sites read almost like a sentence —
genuinely useful once a function takes several parameters and a bare list
of values would otherwise be ambiguous about which value means what.
func greet(name: String) -> String {
return "Hello, \(name)!"
}
print(greet(name: "Chidi"))
Hello, Chidi!