← Swift tutorial

Swift

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.

Example
func greet(name: String) -> String {
    return "Hello, \(name)!"
}

print(greet(name: "Chidi"))
Output
Hello, Chidi!
Ready to go further?

These free tutorials are a preview of what a Code Secure Academy cohort covers, project work and all.

Explore our tracks
Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account