100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Functions in Swift

Learn how to declare, call, and label the parameters of reusable blocks of code called functions in Swift.

Functions & ClosuresBeginner8 min readJul 8, 2026
Analogies

Introduction

A function is a self-contained, named block of code that performs a specific task. Swift functions are declared with the func keyword, can accept input via parameters, and can produce output via a return value. Functions let you break a program into small, reusable, testable pieces instead of repeating the same logic everywhere.

🏏

Cricket analogy: A calculateStrikeRate(runs:balls:) function is a self-contained, named block declared with func that takes runs and balls as input and returns the strike rate, letting the scoring app reuse the same logic for every batsman instead of recalculating by hand each time.

Syntax

swift
func functionName(parameterName: ParameterType) -> ReturnType {
    // function body
    return someValue
}

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

Explanation

Every function has a name, a parameter list in parentheses, and an optional return type introduced by ->. Swift also distinguishes between the argument label (used when calling the function) and the parameter name (used inside the function body). By default the parameter name also serves as the argument label, but you can specify a different one, such as func greet(to name: String), where to is the external label callers must write and name is the internal name used in the body. Writing an underscore _ as the label lets you omit it entirely at the call site, which is common for the first parameter of many standard library functions.

🏏

Cricket analogy: func award(to player: String) -> String uses to as the external argument label callers write, like award(to: "Kohli"), while player is the internal name used in the body; func run(_ overs: Int) with an underscore lets you call run(20) without a label, the way you'd shout a number without ceremony.

Example

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

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

func add(_ a: Int, _ b: Int) -> Int {
    return a + b
}

print(greet(name: "Ava"))
print(greet(to: "Sam"))
print(add(3, 4))

Output

swift
Hello, Ava
Greetings, Sam!
7

Key Takeaways

  • Functions are declared with func, an optional parameter list, and an optional return type.
  • The argument label is what callers write; the parameter name is used inside the function body.
  • Use _ as the argument label to omit it at the call site.
  • A custom argument label can differ from the parameter name, e.g. func greet(to name: String).
  • Functions make code reusable, readable, and easier to test.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#FunctionsInSwift#Functions#Syntax#Explanation#Example#StudyNotes#SkillVeris