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
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
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
Hello, Ava
Greetings, Sam!
7Key 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
1. Which keyword is used to declare a function in Swift?
2. In `func greet(to name: String)`, what is `to`?
3. How do you omit an argument label at the call site?
4. What symbol introduces a function's return type?
5. What does `func add(_ a: Int, _ b: Int) -> Int` allow you to write at the call site?
Was this page helpful?
You May Also Like
Closures in Swift
Understand closures as self-contained blocks of functionality that capture and store references to surrounding variables.
Parameters and Return Values in Swift
Master default values, variadic parameters, in-out parameters, and multiple return values via tuples in Swift functions.
Higher-Order Functions in Swift
Explore functions that take other functions or closures as arguments, such as map, filter, and reduce.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics