Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Swift Dictionary

  1. Home
  2. Swift Dictionary
  3. func / Argument Labels / Default Parameters

func / Argument Labels / Default Parameters

Functions in Swift are defined using the func keyword. You can configure arguments flexibly with argument labels, default parameters, and variadic parameters.

Syntax

// Basic function definition
func functionName(argumentLabel parameterName: Type) -> ReturnType {
    return value
}

// Omitting the argument label (use _)
func functionName(_ parameterName: Type) -> ReturnType { }

// Default parameter
func functionName(parameterName: Type = defaultValue) { }

// Variadic parameter
func functionName(parameterName: Type...) { }

Syntax List

SyntaxDescription
func name() { }Defines a function with no parameters and no return value.
func name(label parameter: Type) { }Defines a function with separate argument label and parameter name.
func name(_ parameter: Type) { }Uses _ to omit the argument label, allowing the caller to pass the value without a label.
func name(parameter: Type = value) { }Specifies a default parameter value, making the argument optional.
func name(parameter: Type...) { }Defines a variadic parameter, received as an array inside the function.
-> TypeSpecifies the return type of the function.
@discardableResultSuppresses the compiler warning when the return value is unused.

Sample Code

// Basic function
func greet(name: String) -> String {
    return "Hello, \(name)!"
}
print(greet(name: "Taro"))

// Separate argument label and parameter name
func move(from start: String, to end: String) {
    print("Moving from \(start) to \(end)")
}
move(from: "Tokyo", to: "Osaka")  // Call using argument labels

// Omit label with _
func multiply(_ a: Int, _ b: Int) -> Int {
    return a * b
}
print(multiply(3, 4))  // Call without labels

// Default parameter
func log(_ message: String, level: String = "INFO") {
    print("[\(level)] \(message)")
}
log("Started")              // [INFO] Started
log("Error occurred", level: "ERROR") // [ERROR] Error occurred

// Variadic parameter (received as an array)
func sum(_ numbers: Int...) -> Int {
    return numbers.reduce(0, +)
}
print(sum(1, 2, 3, 4, 5))  // 15

// Implicit return (single expression)
func square(_ n: Int) -> Int { n * n }  // return can be omitted
print(square(7))  // 49

Notes

Swift functions allow you to separate the argument label (the external name used by callers) from the parameter name (the internal name used in the function body). This enables readable call sites like move(from: "A", to: "B").

In single-expression functions, the return keyword can be omitted (Swift 5.1 and later). Only one variadic parameter (...) is allowed per function. Default parameters are conventionally placed at the end of the parameter list.

For inout parameters and multiple return values, see inout / Return Types / Multiple Return Values.

If you find any errors or copyright issues, please .